• Register
1 vote


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


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();

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();

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

13 9 6
94,240 points
0 votes

First of all, you have to know what is 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.


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();





The number is: 100


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

1 vote
1 answer 42 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
1 vote
1 answer 29 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 24 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 7 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 5.3k points
0 votes
1 answer 20 views
I want to make a static reference to the non-static method.
asked Sep 22, 2020 Daniel Anderson 4k points
1 vote
2 answers 63 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, 2020 Gavin 15.3k points
2 votes
1 answer 64 views
Problem: Learning a new programming language is really fun. So I am doing now. Yeah, java is fun but I stuck somewhere in static reference to the non-static method. What is this? I couldn’t figure it out properly. I have written a program below for ... public void mySecondMethod(){ } } I just tried to call one of my functions inside another function. Any solution is much appreciated. Thanks
asked Mar 25, 2020 Gavin 15.3k points
0 votes
1 answer 27 views
Problem: Hello! I am working with ArrayLists in Java. I have declared an ArrayList and added elements in it but now I want to add another element at a specific index while moving the other elements one index ahead. I do not know how I can do that. What method can I use? Is there any method in ArrayList class that can be used for this purpose or do I have to write a custom method?
asked Nov 30, 2020 Code Learner 5.3k points
3 votes
1 answer 37 views
Problem: As a beginner, I have a very basic level question. This might be very nonsense to you guys but I need to know. Why is final the only modifier for local variables in Java? I tried to declare final variables with modifiers like all others but it ... -time error, you cannot use modifiers on local variables inside a method except final. I am looking forward to having your explanation. Thanks.
asked Mar 23, 2020 Gavin 15.3k points
0 votes
1 answer 47 views
Problem: This Question all of Java builtin standard, built-in objects, including their methods and properties. The term "global objects" (or standard built-in objects) here is not to be confused with the global object. Here, "global objects" refer to objects in the global scope. any one tell me about how to retrives built in objects
asked Nov 3, 2020 Mashhoodch 5.6k points