I'm new to Java. Please help me understand Java.
I have trouble understanding some my teacher's code about "stacks."
The code bellow is for my class Person. My question is what does private Person next; mean? Why this variable such with class name ? What is this class doing?
public class Person {
private String _name, _address;
private int _id;
private Person next; // what is this mean and do
public Person(String a, String b, int c){
this._name = a;
this._address = b;
this._id = c;
}
public Person(){
}
public String getname(){
return this._name;
}
public String getaddress(){
return this._address;
}
public int getid(){
return this._id;
}
public person getnext(){
return this.next;
}
public void setname(String a){
this._name = a;
}
public void setaddress(String a){
this._address = a;
}
public void setid(int a){
this._id = a;
}
public void setnext(person a){
this.next = a;
}
@Override
public String toString(){
return "Person{"+ "Name = " + _name +", Address" + _address +", Id = " + _id +'}';
}
}
And this is my code for main class. In this main class I can't understand what private static Person HeadStack contains and what it will do. Why is HeadStack keyword always used in method "pop", "push", and "print"?
public class mainmahasisswa {
private static Person HeadStack; // what is this mean & do
public static void main(String[] args) {
push();
push();
push();
print();
pop();
print();
pop();
print();
push();
print();
}
private static Person setdata(){
person pr = new person();
String name, address;
int id;
name = JOptionPane.showInputDialog("Name : ");
address = JOptionPane.showInputDialog("Address : ");
id = Integer.parseInt(JOptionPane.showInputDialog("id : "));
pr.setname(name);
pr.setaddress(address);
pr.setid(id);
pr.setnext(null);
return pr;
}
private static void pop(){
if (HeadStack != null){
person aa ;
aa = HeadStack.getnext();
HeadStack = aa;
}
else{
System.out.println();
}
}
private static void push(){
person x = setdata();
if(HeadStack != null){
x.setnext(HeadStack);
HeadStack = x;
}
else{
HeadStack = x;
}
}
private static void print(){
if(HeadStack != null ){
person y = HeadStack;
while(y != null){
System.out.println(y.toString());
y = y.getnext();
}
System.out.println();
}
else{
System.out.println();
}
}
}
String _nameis a reference to an object just likeperson nextis.public class Person {is recommended.