Jump to content
Phishing Emails & YouTube Messages - Fake Giveaway Read more... ×
Search In
  • More options...
Find results that contain...
Find results in...
buklu

number of student plus 1

Recommended Posts

Posted · Original PosterOP

output:0 John Chan          how do i modify the number of student to 1 not 0?

public class Student {

private static int numberOfStudent=0;

public String name;

 

 

 

public Student(String name) {

 

setName(name);

}

 

public Student(int numberOfStudent) {

 

numberOfStudent++;

}

 

 

public void setName(String name) {

this.name=name;

}

public int getNumberOfStudent() {

return numberOfStudent;

}

 

 

}

 

 

 

 

public class TestStudent {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

Student stu1=new Student("John Chan");

 

 

 

System.out.println(stu1.getNumberOfStudent());

System.out.println(stu1.name);

}

 

}

Link to post
Share on other sites

You are overloading the constructor with two different signatures ( Student(String) and Student(int) ). When you call it, it uses the fitting constructor ( in this case Student(String) ). If you want to increment the counter on every object creation, you need to do it inside the same constructor:
 

public Student(String name) {
  numberOfStudent++;
  
  setName(name);
}

 

Also please post code in a code box.

Link to post
Share on other sites
On 2/7/2019 at 2:42 PM, buklu said:

output:0 John Chan          how do i modify the number of student to 1 not 0?


public class Student {
	private static int numberOfStudent=0;
	public String name;

	public Student(String name) {
		setName(name);
	}

	public Student(int numberOfStudent) {
		numberOfStudent++;
	}

	public void setName(String name) {
		this.name=name;
	}

	public int getNumberOfStudent() {
		return numberOfStudent;
	}
}

public class TestStudent {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Student stu1=new Student("John Chan");

		System.out.println(stu1.getNumberOfStudent());
		System.out.println(stu1.name);
	}
}

 

Public service for those who needed the code in brackets.

 

Second of all - Why are you using your setName() in your constructor? :)  I would define the name value in the Student class as private - use the constructor to set the name on creation of the object and then use the public function setName(String name) to change the name later on and make a getName() function that return the name, if necessary. Like so:

 

public class Student {

	private static int numberOfStudent=0;
	private String name;


	public Student(String name) {
    		numberOfStudent++;
		this.name = name;
	}

	public String getName() {
    		return this.name;
    	}
    
	public void setName(String name) {
		this.name=name;
	}

	public int getNumberOfStudent() {
		return numberOfStudent;
	}
}

 Then your call to the testclass would be:

 

public class TestStudent {

	public static void main(String[] args) {
		Student stu1 = new Student("John Chan");

		System.out.println(stu1.getNumberOfStudent());
      	  	// usage of getName function
		System.out.println(stu1.getName());
	}
}

Hope this helps


A simple software developer from the far away land of Denmark

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Buy VPN

×