• Register
1 vote
42 views

Problem:

Hello java programmers,

Myself is a new student in java. I am practicing basic java methods and objects. Finding beginner-level problems and trying to solve them. I recently got a line that says, all local variables in a method have default values. What does it mean? Can you please write a simple code that makes me understand this well?

Thanks guys.

12 7 7
15,250 points

Please log in or register to answer this question.

2 Answers

1 vote
 
Best answer

Solution:

Before everything else, we need to know:

# A local variable is declared in methods, constructors, or blocks.

# A local variable is visible only within the declared method, constructor, or block.

# Access modifiers cannot be used for local variables

See the program below:

public class Gavin {
   public void myAge() {
      int age;
      age = age + 5;
      System.out.println("My age is : " + age);
   }

   public static void main(String args[]) {
      Gavin gavin = new Gavin();
      gavin.myAge();
   }
}

The following program will produce an error because I didn’t initialize the local variable before I use it. So I need to write the program this way:

​public class Gavin {
   public void myAge() {
      int age=20;
      age = age + 5;
      System.out.println("My age is : " + age);
   }
   public static void main(String args[]) {
      Gavin gavin = new Gavin();
      gavin.myAge();
   }
}

Now the program looks perfect and produces the age 25 as an output. I hope things are clear to you now.
Thanks.

13 9 6
94,240 points
0 votes

First of all, you have to know what is variable?

Variable:

A variable provides us with named storage that our programs can manipulate. Each variable in Java has its own specific type, which determines the size and layout of the memory variable.

There are many kinds of variable but we will discuss local variables.

Local variables:

  • Local variables are those variables, declared in methods, constructors, or blocks.
  • Local variables are created when the methods, constructors, or blocks are entered and the variables are destroyed when it exits from the method, constructor, or block.
  • Access modifiers can not be used for local variables.
  • Local variables can only be used with the declared methods, constructors, or blocks.
  • The implementation is done at the stack level internally.
  • There is no default value for the local variables. So, the local variables should have to declare and an initial value should be assigned before the first use.

Example:

public class LocalVariableTest {

   public void print() {

      int num = 100;

      System.out.println("The number is : " + num);

   }

   public static void main(String args[]) {

      LocalVariableTest obj = new LocalVariableTest();

      obj.print();

   }

}

Output:

The number is: 100

Note:

Local variables can be initialized with a value. If ones try to do this the following error can occur;

“Variable num might not have been initialized.”

 

11 5 2
3,890 points

Related questions

0 votes
1 answer 14 views
14 views
Problem: Hi there! I am a beginner. I saw this program on Internet: class test { private int variable; public test() { variable = 9; System.out.println("Value of variable is " + variable); } } public class Main { public static void main(String[] args) ... following output: Hello World Value of variable is 9 I am unable to understand the output of this program. Can anybody please explain it for me?
asked Mar 7 Code Learner 9.2k points
1 vote
1 answer 47 views
47 views
Problem: Well, I am a new student of java programming. Nowadays I am learning how to use multiple classes in a java program. Based on my learning I’ve written a code that gave me an error, “Invalid method declaration; return type required” I am attaching the ... * Math.PI * radius; return c; } public double radius; } Can you, please tell me where I am doing the wrong thing?
asked Mar 9, 2020 Gavin 15.3k points
1 vote
1 answer 84 views
84 views
Problem: Are the all data type has default values in Java? I am just wondering what is the default value of an integer if I write: int a; boolean b; System.out.println(a); System.out.println(b); All data fields in an object have default values or not? If they have default values what are they? Thanks
asked Mar 26, 2020 Gavin 15.3k points
0 votes
1 answer 3 views
3 views
Problem: Can anyone help me? I have problem regarding method range of object
asked Mar 27 Madhav96 2.3k points
0 votes
1 answer 2 views
2 views
Problem: Is there someone who can assist me in resolving this issue “In spark-shell, which all contexts are available by default”?
asked Mar 30 tuhin1 48.9k points
0 votes
1 answer 21 views
21 views
Problem: Hello there! I am working with C# in Visual Studio and I am learning user-defined functions in C#. When I try to execute my program, it throws an error that says method must have a return type. I have checked my function and I find nothing wrong with it. ... ;t know what’s the problem and what should I do. If someone could tell me, that would be a big favor. Thanks in advance
asked Oct 16, 2020 Code Learner 9.2k points
0 votes
1 answer 12 views
12 views
Problem: hi i am a beginner and i want explain to me what mean this with example
asked Feb 9 ahmedadel9090 3.1k points
1 vote
1 answer 28 views
28 views
Problem: Hello Kodlogs, I find this website very helpful besides the StackOverflow. You guys are really awesome and friendly. I want to know from you guys, how to pass an array to a method? Also what happens when an array is passed to a method? Thanks for your valuable and patient answer.
asked Mar 31, 2020 Gavin 15.3k points
0 votes
1 answer 8 views
8 views
Problem: Hi there! I want the answer to following MCQ: Which of the following is not part of a method call? method name return type parentheses all of the above are part of a method call Thanks!
asked Mar 6 Code Learner 9.2k points
0 votes
1 answer 36 views
36 views
I want to make a static reference to the non-static method.
asked Sep 22, 2020 Daniel Anderson 4k points