-1

I an initializing value to one object and then declaring the other object of the same class but the value of the first object by default assign to the second object not the default null values.

    public class Student {

static String name;
static String fatherName;
public Student(String name,String fatherName)
{
    this.name=name;
    this.fatherName=fatherName;
}
public Student() {
}
public static void showName(){
    System.out.println("Hello my name is : "+name+"\nMy father name is: "+fatherName);
}

}

the is the class which store information about student.

    public class Test {
public static void main(String[] args) {
    Student st1=new Student("xyz","abc");
    st1.showName();
    Student st2=new Student();
    st2.showName();

}

} here i created two object first is initializing the first object(st1) and then I decleared the second object with default object and on both I called the same object why the method called with second object is printing the value passed to the first why it not print the default null value.

Output:

"C:\Program Files\Java\jdk-16.0.2\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2020.3.2\lib\idea_rt.jar=57206:C:\Program Files\JetBrains\IntelliJ IDEA 2020.3.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Users\Muhammad Ismail\IdeaProjects\oop in java\out\production\oop in java" Test

    Hello my name is : xyz
    My father name is: abc
    Hello my name is : xyz
    My father name is: abc

    Process finished with exit code 0
  • That's because `name` and `fatherName` are both static, so not fields of the individual objects. Remove the `static` qualifier and you should see the behavior you expect. – OhleC Feb 14 '22 at 13:06
  • Note: the real answer here is: in programming, *every* character in your code matters. You have to **understand** what the keywords you put in your code are doing. Especially when you are a beginner, learning everything with "trial and error" isn't an efficient strategy. Instead: pick up a good book, and follow that, instead of putting together some code, and then wondering why it doesn't at all do what you think it should do. – GhostCat Feb 14 '22 at 13:17

2 Answers2

0

You defined the variables as static. This makes them a class variable rather than an instance variable. Remove the keyword static from the variable definition and it should behave as intended.

public class Student {
  private String name;
  private String fatherName;
  public Student(String name,String fatherName) {
    this.name=name;
    this.fatherName=fatherName;
  }
  public Student() {}

  public static void showName(){
    System.out.println("Hello my name is : "+name+"\nMy father name is: "+fatherName);
  }
}
vsfDawg
  • 1,425
  • 1
  • 9
  • 12
-1

Because you used static keyword to declare attributes of the class. Replace those with private keyword.

  • Note: private has nothing to do with static vs non-static. Thus "replacing" isn't the key here, it is about the presence (or non presence) of static. private is only about visibility, that has NOTHING to do with the bug the OP ran into. – GhostCat Feb 14 '22 at 13:18