• 0

[Java] What does initialize mean? What is void?


Question

Hi. I'm learning to write Java applet and I have got some questions.

Take a look of part of the source code of an Applet:

import javax.swing.*;
import java.awt.Graphics;

public class AdditionApplet extends JApplet{

  double sum;

  public void init( )
  {
	String firstNumber,
		  secondNumber;

	double number1,
		  number2;
		  //Remark 01
  }
// To be continued...
}

Here are my questions:

1. Why should we "double sum" at the beginning? My book said that it is used to initialize the variable and to be used within the class. But any more reason we should initialize it?

2. For remark 01. Why don't we have to put the variable sum here? Just becoz we already defined that sum is a type of double?

3. What is void? Does it mean "return nothing when the statement is correctly executed"?

Thanks for your kindful help.

3 answers to this question

Recommended Posts

  • 0
  Gundamdriver said:

Hi. I'm learning to write Java applet and I have got some questions.

Take a look of part of the source code of an Applet:

import javax.swing.*;
import java.awt.Graphics;

public class AdditionApplet extends JApplet{

  double sum;

  public void init( )
  {
	String firstNumber,
		  secondNumber;

	double number1,
		  number2;
		  //Remark 01
  }
// To be continued...
}

Here are my questions:

1. Why should we "double sum" at the beginning? My book said that it is used to initialize the variable and to be used within the class. But any more reason we should initialize it?

2. For remark 01. Why don't we have to put the variable sum here? Just becoz we already defined that sum is a type of double?

3. What is void? Does it mean "return nothing when the statement is correctly executed"?

Thanks for your kindful help.

From my own limited knowledge of c++ (which i know is similar in syntax)

1. you are defining "sum" as a variable of type double within the scope of the class, so any routine within the class can use/alter its contents directly.

2. for design purposes, you would then limit direct access to "sum" within the subroutine Init()

3. I "think" you are right, like "sub" in VB

  • 0
  Gundamdriver said:

Hi. I'm learning to write Java applet and I have got some questions.

Take a look of part of the source code of an Applet:

import javax.swing.*;
import java.awt.Graphics;

public class AdditionApplet extends JApplet{

  double sum;

  public void init( )
  {
	String firstNumber,
		  secondNumber;

	double number1,
		  number2;
		  //Remark 01
  }
// To be continued...
}

Here are my questions:

1. Why should we "double sum" at the beginning? My book said that it is used to initialize the variable and to be used within the class. But any more reason we should initialize it?

2. For remark 01. Why don't we have to put the variable sum here? Just becoz we already defined that sum is a type of double?

3. What is void? Does it mean "return nothing when the statement is correctly executed"?

Thanks for your kindful help.

1. The definition of "initialize" means to assign a value when the variable is created. What that code is actually doing is simply declaring a variable called "sum" that is of the primitive data type "double", which is a double-precision floating point (FP) number. Typically, you create a variable inside the class itself to make it available to all methods that belong to the class. In this case, "sum" can be referenced from any method, including main.

2. Your assumption is correct. You can't define "sum" at remark 01 because "sum" was already defined previously. As jetblack stated, defining "sum" at remark 01 would mean you could only use "sum" in the method "init"

3. "void" when defined as a method's return type simply means the method does not return anything. In this case, the void method "init" simply creates variables. There should be no problem with this, so there is no reason to use something like "boolean" or "int" in place of "void" to check for errors.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Install a cheap Chinese clone of Adguard and then take support from call center to restore to Windows XP.
    • Does the job, although it can't run windows 11 officially. Currently running Windows 10 modded with ReviOS
    • Now, there is a dilemma - should I install some Russian software ( AdGuard ) to stop US ( Microsoft ) to spy on me ?
    • AdGuard is yet another app to block Windows Recall by Taras Buria Windows Recall is one of the most controversial Windows features. After the misfired launch and the scandal that followed, Microsoft implemented plenty of security measures and improvements. Still, some believe that Recall's reputation is tarnished forever. Besides, not everyone is comfortable with using the feature that takes screenshots of everything you do. As such, privacy-focused apps, browsers, and messengers are now offering blocks for Recall. AdGuard is the latest one to join them. AdGuard for Windows 7.21 introduces a new feature that lets you turn off Windows Recall. Although Recall is a strictly opt-in experience and it has several security measures, AdGuard developers believe that it is not enough. They argue that the feature idea itself is unsettling, PINs are easy to crack, and filters sometimes fail to engage. Here is what AdGuard says in the announcement post: If you use AdGuard on your PC, you can find the new "Disable Windows Recall" feature in Settings > Tracking Protection. As of now, Recall is only available on Copilot+ PCs. This month's non-security update expanded Recall to more users, making the feature available in the European Economic Area. And with Intel working on the next-generation desktop processors with improved NPUs, you can expect Recall and other recently introduced AI features to make their way to more users with desktop PCs. As an online publication, Neowin relies on ads for operating costs, and if you use an ad blocker, we'd appreciate being whitelisted. In addition, we have an ad-free subscription for $28 a year, which is another way to show support!
    • I'm sorry for sounding rude. I was just frustrated. I really want to get this right because I don't want to take any risk of potentially contaminating other hardware and risk losing precious files. Thanks again for the help. 
  • Recent Achievements

    • One Month Later
      Philsl earned a badge
      One Month Later
    • One Year In
      armandointerior640 earned a badge
      One Year In
    • One Month Later
      armandointerior640 earned a badge
      One Month Later
    • One Month Later
      Itbob513626 earned a badge
      One Month Later
    • Week One Done
      Itbob513626 earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      615
    2. 2
      ATLien_0
      236
    3. 3
      Xenon
      156
    4. 4
      +FloatingFatMan
      122
    5. 5
      Michael Scrip
      116
  • Tell a friend

    Love Neowin? Tell a friend!