Results 1 to 20 of 27
Thread: Using a Constructor in Java
- 05-14-2012, 01:50 AM #1
Member
- Join Date
- Apr 2012
- Posts
- 88
- Rep Power
- 0
-
Re: Using 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.
- 05-14-2012, 02:02 AM #3
Member
- Join Date
- Apr 2012
- Posts
- 88
- Rep Power
- 0
Re: Using a Constructor in Java
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:
Java Code://This is creating the constructor and initiliazing the variables, Make, Model and Trans public Car() { Make = Ford; Model = Mustang; Trans = Manual; }
-
Re: Using a Constructor in Java
- 05-14-2012, 02:12 AM #5
Member
- Join Date
- Apr 2012
- Posts
- 88
- Rep Power
- 0
Re: Using a Constructor in Java
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?
-
Re: Using a Constructor in Java
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.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.
- 05-14-2012, 02:22 AM #7
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
Re: Using a Constructor in Java
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.
Website: Learn Java by Examples
- 05-14-2012, 03:25 AM #8
- 05-14-2012, 03:31 AM #9
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
- 05-14-2012, 08:48 PM #10
Member
- Join Date
- Apr 2012
- Posts
- 88
- Rep Power
- 0
Re: Using a Constructor in Java
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?
Java 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()); } }
- 05-14-2012, 09:17 PM #11
Re: Using a Constructor in Java
Does it compile?
Does it run?
Does it perform as expected?
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 05-14-2012, 09:30 PM #12
Member
- Join Date
- Apr 2012
- Posts
- 88
- Rep Power
- 0
Re: Using a Constructor in Java
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
-
Re: Using a Constructor in Java
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".
- 05-15-2012, 03:11 AM #14
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
Re: Using a Constructor in 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.
Website: Learn Java by Examples
- 05-15-2012, 11:00 PM #15
Member
- Join Date
- Apr 2012
- Posts
- 88
- Rep Power
- 0
Re: Using a Constructor in Java
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)
-
Re: Using a Constructor in Java
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.
- 05-16-2012, 02:54 AM #17
Member
- Join Date
- Apr 2012
- Posts
- 88
- Rep Power
- 0
Re: Using a Constructor in Java
Code:
List of errors:Java 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
-
Re: Using a Constructor in Java
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.
- 05-16-2012, 03:05 AM #19
Member
- Join Date
- Apr 2012
- Posts
- 88
- Rep Power
- 0
Re: Using a Constructor in Java
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.
-
Re: Using a Constructor in Java
Similar Threads
-
How to call a master constructor from a minor constructor?
By b.m in forum New To JavaReplies: 5Last Post: 12-14-2011, 01:47 PM -
Java Constructor Question
By ashly in forum New To JavaReplies: 6Last Post: 03-09-2010, 02:10 AM -
Calling constructor of parent class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:10 AM -
Calling constructor of same class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:01 AM -
Help with constructor in java
By mathias in forum New To JavaReplies: 1Last Post: 08-07-2007, 01:00 AM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks