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