How do you set up/use a constructor in java?
Printable View
How do you set up/use a constructor in java?
Come on now -- the basic tutorials will surely show you how to do this. Rather than have us re-write one of these, why don't you tell us exactly what confuses you.
Providing Constructors for Your Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
I have read this article repeatedly and I think what it says the following would be true:
Code://This is creating the constructor and initiliazing the variables, Make, Model and Trans
public Car()
{
Make = Ford;
Model = Mustang;
Trans = Manual;
}
I haven't begun writing the code yet, I was trying to wrap my mind around the constructor piece before I began.
The difference that I see between my sample code, and the code I was trying to copy from is that my code does not receive any parameters, whereas the code from the site receives 3 parameters?
Better to write the code before you come here. We're no substitute for your compiler, and you really can't break the compiler or computer with experimentation. Yes you'll get errors, but by trying to understand the errors and working with the code, you'll gain a much deeper appreciation for compilers than if we spoon-fed you an answer.
Correct.Quote:
The difference that I see between my sample code, and the code I was trying to copy from is that my code does not receive any parameters, whereas the code from the site receives 3 parameters?
Happy coding.
Constructor is a special method that allows you to create an instance of a class. Just like a method you can declare a constructor to accept parameters. The only different is that a constructor does not have a return type and it must have a name that match the class name.
Thanks for the correction DB :)
So I have spent about the past 8 hours reading on Constructors and examples and trying to wrap my mind around it. I think I am finally getting it. Expanding on my Car example, I horribly coded wrong yesterday evening...Does this coding look correct to the master coders here?
Code:Public Class Car
{
String make;
String model;
String trans;
public int getInfo()
{
return make + model + trans;
}
Car()
{
make = Ford;
model = Mustang;
trans = Manual;
}
Car(string m, string, mo, string t)
{
make = m;
model = mo;
trans = t;
}
Public Static void main (String[] args)
{
Car carObj1, carObj2;
carObj1 = new Car();
carObj2 = new Car(Ford, Mustang, Manual);
System.out.println("The Make, Model and Transmission of the car is: " + carObj1.getInfo());
}
}
Does it compile?
Does it run?
Does it perform as expected?
db
Guess I am not understanding it as I thought I was. No it will not compile...below are my errors:
C:\Programs>javac test.java
test.java:1: error: class, interface, or enum expected
Public Class Car
^
test.java:4: error: class, interface, or enum expected
String model;
^
test.java:5: error: class, interface, or enum expected
String trans;
^
test.java:7: error: class, interface, or enum expected
public int getInfo()
^
test.java:10: error: class, interface, or enum expected
}
^
test.java:15: error: class, interface, or enum expected
model = Mustang;
^
test.java:16: error: class, interface, or enum expected
trans = Manual;
^
test.java:17: error: class, interface, or enum expected
}
^
test.java:22: error: class, interface, or enum expected
model = mo;
^
test.java:23: error: class, interface, or enum expected
trans = t;
^
test.java:24: error: class, interface, or enum expected
}
^
test.java:29: error: class, interface, or enum expected
carObj1 = new Car();
^
test.java:30: error: class, interface, or enum expected
carObj2 = new Car(Ford, Mustang, Manual);
^
test.java:31: error: class, interface, or enum expected
System.out.println("The Make, Model and Transmission of the car is: " +
carObj1.getInfo());
^
test.java:32: error: class, interface, or enum expected
}
^
15 errors
Your class name must match the name of the file. Thus if you've got a class name of "Car" then the file name must be "Car.java".
And remember that Java is case sensitive language, so lowercase and uppercase is a different thing. For instance when you declare your class you are using a wrong keyword, Public vs public.
I knew the file name had to be the same name...I had just forgotten that. This has been updated so the file is named Car.java and the class is named Car.
I did not know that java was case sensitive! Thank you for pointing that out.
Now with that being said and both of those obvious issues being fixed, I am still getting the same compile errors as I did before (post 12)
If you're still having problems, even if they seem the same, please re-post your code with code tags and the full list of errors.
Code:
List of errors:Code:Public Class Car
{
String make;
String model;
String trans;
public int getInfo()
{
return make + model + trans;
}
Car()
{
make = Ford;
model = Mustang;
trans = Manual;
}
Car(string m, string, mo, string t)
{
make = m;
model = mo;
trans = t;
}
public static void main (String[] args)
{
Car carObj1, carObj2;
carObj1 = new Car();
carObj2 = new Car(Ford, Mustang, Manual);
System.out.println("The Make, Model and Transmission of the car is: " + carObj1.getInfo());
}
}
c:\test>javac car.java
car.java:1: error: class, interface, or enum expected
Public Class Car
^
car.java:4: error: class, interface, or enum expected
String model;
^
car.java:5: error: class, interface, or enum expected
String trans;
^
car.java:7: error: class, interface, or enum expected
public int getInfo()
^
car.java:10: error: class, interface, or enum expected
}
^
car.java:15: error: class, interface, or enum expected
model = Mustang;
^
car.java:16: error: class, interface, or enum expected
trans = Manual;
^
car.java:17: error: class, interface, or enum expected
}
^
car.java:22: error: class, interface, or enum expected
model = mo;
^
car.java:23: error: class, interface, or enum expected
trans = t;
^
car.java:24: error: class, interface, or enum expected
}
^
car.java:26: error: class, interface, or enum expected
public static void main (String[] args)
^
car.java:29: error: class, interface, or enum expected
carObj1 = new Car();
^
car.java:30: error: class, interface, or enum expected
carObj2 = new Car(Ford, Mustang, Manual);
^
car.java:31: error: class, interface, or enum expected
System.out.println("The Make, Model and Transmission of the car is: " +
carObj1.getInfo());
^
car.java:32: error: class, interface, or enum expected
}
^
16 errors
This error:
c:\test>javac car.java
suggests that either your file is named car.java rather than Car.java or that you're typing in car.java when you should be typing in Car. java when calling javac.exe. Remember that capitalization matters.
The name of my file is car.java (I just double checked to make sure :) )
other files that I want to open, I always type javac filename.java to create my class file, then type java filename to execute the program.