• Register
1 vote
30 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

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 1
3,890 points

Related questions

1 vote
1 answer 29 views
29 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 Gavin 15.3k points
1 vote
1 answer 20 views
20 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 Gavin 15.3k points
1 vote
1 answer 19 views
19 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 Gavin 15.3k points
0 votes
1 answer 6 views
6 views
I want to make a static reference to the non-static method.
asked Sep 22 Daniel Anderson 4k points
1 vote
2 answers 43 views
43 views
Problem: Hello Kodlogs, I have a simple java code that I wrote a few minutes back I was trying to compare numbers using methods. The program throws the error again and again: this method must return a result of type int. I couldn’t figure out the problem. Could you please shed some light on this ... []) { int x = 8; int y = 9; int z = 20; System.out.println(largest(x, y, z)); } } Many thanks
asked Mar 27 Gavin 15.3k points