0

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();
        }
    }
}
2
  • BTW String _name is a reference to an object just like person next is. Commented Jul 4, 2014 at 6:51
  • Please note the the recommended naming convention for java class names is: "Class names should be nouns, in mixed case with the first letter of each internal word capitalized.". See Code Conventions for the Java TM Programming Language. So in your case public class Person { is recommended. Commented Jul 4, 2014 at 7:44

1 Answer 1

1

Think of it as a link in the chain private person next provides a link to the "next" person object in this chain or null if it does not exist.

private person next; // what is this mean and do 

This a declaration that says next is a type of person, which is initially assigned null

This...

private static  person HeadStack; // what is this mean & do 

is simply a declaration that says HeadStack is type of person which is static, meaning that it doesn't rely on particular instance of any class to be accessed.

Take a look at Language Basics: Variables and Understanding Class Members

why this HeadStack keyword always used in method "pop","push","print"

The object represents the first link in the chain

Sign up to request clarification or add additional context in comments.

2 Comments

Still don't understand, about why person in (private person next) can be variable. Like int to contain number.
Why not? person is just a type of object, it's not an instance, so the declaration goes something like {modifier} {type} {name}, which says that next is a type of person and is private to the person class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.