Reputation: 1615
I have a main class, what looks like this:
class Main {
public static void main (String[] args) {
Mobil one = new Mobil ("xxxxxx", "yyyyyy", 00000001, true);
Mobil two = new Mobil ("yyyyyy", "xxxxxx", 10245624, false);
one.touchcontrol();
two.touchcontrol();
}
}
And I have this Mobil class:
class Mobil {
String type;
String manufactureat;
int modellnumber;
boolean touchtype;
public Mobil (String manufacturer, String inittype, int number, boolean touch) {
manufacturer = manufactureat;
inittype = type;
number = modellnumber;
touch = touchtype;
}
public void touchcontrol() {
if (touchtype == false)
{
System.out.println("This model, has not got Touchscreen!");
}
else
{
System.out.println("This model, has Touchscreen!");
}
}
But when I run the program and invoke the one.touchcontrol();
and two.touchcontrol();
it shows that no model has got Touchscreen. I don't know what I missed.
Upvotes: 0
Views: 376
Reputation: 3574
You are incorrectly assigning values to variables in your constructor...
public Mobil (String manufacturer, String inittype, int number, boolean touch) {
manufacturer = manufactureat; // should be manufactureat = manufacturer;
inittype = type; //same problem
number = modellnumber; // same here
touch = touchtype; // and here
}
Upvotes: 1
Reputation: 1108642
You need to swap the variable assignments in the constructor.
manufactureat = manufacturer;
type = inittype;
modellnumber = number;
touchtype = touch;
In variable assignments in Java (and in pretty much all other languages), the left hand will retrieve the value of the right hand.
Upvotes: 7