Quick Question: Did I understand these directions correctly?
I'm new to java. I just need a little clarification.
1. Create a class called Student that has name, grade, house as instance variables
2. Create a constructor that take these (3) items as parameters and sets them to your private instance/global variables
My code:
Code :
public class Student {
String name;
int grade;
String house;
public Student(String studentName, int studentGrade, String studentHouse)
{
name = studentName;
grade = studentGrade;
house = studentHouse;
}
The one thing that I'm confused about is that I looked at the java tutorial for making constructors. It was a bicycle example. The instance variables for the Bicycle class (public int cadence, public int gear, public int speed) were NOT the same as the parameters in the constructor(int startCadence, int startSpeed, int startGear). Why is this? My assignment seems to want me to make my constructor parameters the same as my instance variables.
Re: Quick Question: Did I understand these directions correctly?
The same names you mean? You could pass this as a parameter, not as a formal parameter, but when calling it:
Student s = new Student("Bob", 100, "Smith");
It doesn't have to be the same name when calling it as an object.
Actually, they do have to be same data type and in the same order.
Also, you can't do this in a class for instance:
public JSeparatedMenu(int value)
{
}
public JSeparatedMenu(int value2)
{
}
Though the two constructors have different variable names, they both have only one variable, an int one in each case. It won't compile as it already says I have a constructor that takes an int as its only parameter.