Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Java
public class test {
	private Node head;
	
	private class Node{
		int p;
		Node next;
		
		public Node(int p, Node next) {
			this.p = p;
			this.next = next;
		}
	}
	
	public void set() {
		set(head);
	}
	
	private void set(Node p) {
		p = new Node(1, null);
	}
	
	public boolean isEmpty() {
		return head == null;
	}
	
	public static void main(String[] args) {
		test a = new test();
		a.set();
		System.out.println(a.isEmpty());
	}
}


the set() fuction calss set(head), then i think p = head, and the private set(Node p)

if to initialize the head, but the fact is that head is not initialized, so i what to know why
Posted

1 solution

Java passes function parameters by value, not by reference - so when you pass head to the set function, it copies the current value of the variable, and passes that value - any changes you make to the value within the function affect only the copy and are not reflected by into the original variable.

In order to do what you want, you would need to return the new value from the function and set head to that.
 
Share this answer
 
Comments
Member 11947134 31-Aug-15 8:30am    
thank you for your answer XD
OriginalGriff 31-Aug-15 8:34am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900