-7

The output of the program written is Null. I cant find where is the error in here? Can anyone explain the following code and why does it return Null?

class methodexample 
{ 
    int number; 
    String name; 

    void data() 
    {
        int number=5; 
        int name="Rahul";
    } 

    void display() 
    { 
        System.out.println("Number is" +number); 
        System.out.println("Name is " +name);
    } 

    public static void main(String args[]) 
    {
        methodexample m=new methodexample(); 
        m.data();
        m.display();
    }
 }
Tiago_nes
  • 843
  • 7
  • 30
  • 3
    Please read https://stackoverflow.com/editing-help, then add *text* in your question as well as just code. The text shouldn't all be in the title. You should also be more precise - when you say "It is known that we cannot declare variable two times in java", it's perfectly allowable to declare a local variable with the same name as an instance variable, which is what you've done. – Jon Skeet Dec 11 '17 at 14:44
  • 3
    your data() function assigns values to local variables, not to class variables – Ronald Dec 11 '17 at 14:45
  • Your fields are declared in two different scopes, and the ones getting assigned are local, while the ones printed out are not assigned, and therefore are set to their default values. – Mena Dec 11 '17 at 14:45
  • Possible duplicate of [Why does Java throw NullPointerException here?](https://stackoverflow.com/questions/30567802/why-does-java-throw-nullpointerexception-here) (Lrrrs answer explains the issue) – Tom Dec 11 '17 at 14:46
  • 3
    It's vastly oversimplified to say you can't declare 2 variables with the same name. You could have 2 variables with the same name, but in 2 different methods. In your case, it's between class variables and local (method) variables, which are in different scopes. – AntonH Dec 11 '17 at 14:46
  • Thanks a lot.. I am a newbie.. – Shilpi Negi Dec 11 '17 at 14:50
  • You should read up on variable scope. https://www.java-made-easy.com/variable-scope.html – trappski Dec 11 '17 at 14:52

3 Answers3

0

"void data()" changes the global variables in its scope, but not globally. So, if you call display(), for it the variables didnt got changed - to change them in your intrest, you can let the data()-method return the string and the int: (dont use int for "rahul" tho)

int number = m.setNumber()
int name = m.setName()
int setNumber(){
return 5;
}
int setName(){
return "Rahul";
}
Reisbrot
  • 37
  • 9
0

The variables declared in void data() are local to that method and can only be referred to in that method, so the 2 variables you set at the top are still null.

class methodexample {

    int number; 
    String name;

    void data() {

        number=5;
        name="Rahul";

    }

    void display() {

        System.out.println("Number is " + number);
        System.out.println("Name is " + name);

    }


    public static void main(String args[]) {

        methodexample m=new methodexample();

        m.data();

        m.display();

    }
}
JoJo
  • 117
  • 3
  • 12
0
  1. Class names should not start with small letter. Check Java conventions for class names
  2. when you declare variables at class level, they are refereed as instance variables. So whenever you create object of that class every object will get its own copy of those variables.
  3. Instance variables default value is set to null;
  4. When you create variables inside a function then those are called local variables. There scope is limited to that function. So once the function is executed those variables are not longer accessible
  5. In your case since you have called method using object reference it will refer to instance variables. For String variables default value is null, as far as i know for integer variable if used as instance variable it will have default value as 0
Rohit
  • 146
  • 1
  • 5