Home > Mobile >  Could someone clarify why the firstname is being printed as null in the below code?
Could someone clarify why the firstname is being printed as null in the below code?

Time:10-05

Since it is a non-static variable, I'm trying to understand how we should call this class variable in both static & non-static method.

Code:

public class Person {
    String firstName;

public static void main(String[] args) {
        Person pps = new Person();
        pps.setFirstName("Surendra");
        System.out.println(getFirstName());
}

public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

public static String getFirstName() {
        Person pps = new Person();
        return pps.firstName;
    }

}

CodePudding user response:

Non-static data members cannot be accessed from static methods. Static methods are not attached to any instance of the class, and so cannot give you information about any specific instance.

To access the datamember from a specific instance, you need a non-static method called on the specific instance.

public String getFirstName() {
    return this.firstName;
}

public static void main(String[] args) {
    Person pps = new Person();
    pps.setFirstName("Surendra");
    System.out.println(pps.getFirstName());
}

If you'd like the same firstname to be shared between all instance of the class, then everything should be static:

public class Person {

    static String firstName;

    public static void main(String[] args) {
        Person.setFirstName("Surendra");
        System.out.println(Person.getFirstName());
    }

    public static void setFirstName(String newFirstName) {
        firstName = newFirstName;
    }

    public static String getFirstName() {
        return firstName;
    }

}

CodePudding user response:

The problem is that you are creating a new Person within your getter which makes your previous code within your Main method irrelevant because you are querying the first name of the second Person which was never set. Since your static method does not have access to instances, you will need to pass in the instance.

This is what your code should look like if you want a static method to return an instance variable -

public static String getFirstName(Person pps) {
        return pps.firstName;
    }

public static void main(String[] args) {
    Person pps = new Person();
    pps.setFirstName("Surendra");
    System.out.println(Person.getFirstName(pps));
}

PS- the other 2 answers are correct on how you should normally set/get a variable but it seems to me that you are purposefully experimenting with static and member methods. If I misunderstood then follow either of their answers instead

PPS - also its a best practice for your main method to be in its own class.

CodePudding user response:

Static variables are declared inside a class and can be used outside the class with the class name and the class object. But the class name is preferred.

Non-static variable can’t be used inside a static block or a static method but static variable can be used inside non-static as well as static methods.

So, either 'firstName' as a static variable. or make the getFirstName as a non-static method as follows.

     String firstName;

public static void main(String[] args) {
    StackOverFlowTesting pps = new StackOverFlowTesting();
        pps.setFirstName("Surendra");
        System.out.println(pps.getFirstName());
}
public  String getFirstName() {
    //StackOverFlowTesting pps = new StackOverFlowTesting();
        return this.firstName;
    }
public void setFirstName(String firstName) {
    this.firstName = firstName;
}

CodePudding user response:

Try removing the Static part from getFirstName and then change the println to call the getFirstName method from the created Person object.

public class Person {
        String firstName;
    
    public static void main(String[] args) {
            Person pps = new Person();
            pps.setFirstName("Surendra");
            System.out.println(pps.getFirstName());
    }
    
    public void setFirstName(String firstName) {
            this.firstName = firstName;
        }
    
    public String getFirstName() {
            Person pps = new Person();
            return pps.firstName;
        }
    
    }
  • Related