• Post Reply Bookmark Topic Watch Topic
  • New Topic

New class and constructors  RSS feed

 
John Vorona
Ranch Hand
Posts: 48
Java Mac OS X Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I am learning how to create a new class and establish constructors as well as methods for that class. Note: This is not the main class, therefore i do not have the main method.
The program i am trying to make is to simply output the type of car, the model, and the car's year onto the screen when requested.
Below is my source code of the Cars.java file, I am having some compiling issues and I am sure I am doing something wrong, if somebody more advanced could help me out and point out my mistakes, this will guide me in the right direction.

Anyways, here is my source code:



I compile this via Command Prompt as I am on windows 7, the error messages i receive are as follows:


 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Here's a couple of tips to help you get the most out of the Ranch.

Firstly, please be sure to use code tags when posting code to the forums. Unformatted or unindented code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please click this link ⇒ UseCodeTags ⇐ for more information. Properly indented and formatted code greatly increases the probability that your question will get quicker, better answers.

I've gone ahead and added the code tags for you. See how much easier the code is to read?

Secondly, never just tell us "there are errors". Show us.

Cut and paste the exact error messages, and be sure to UseCodeTags for them too so that the formatting is preserved.
 
John Vorona
Ranch Hand
Posts: 48
Java Mac OS X Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Welcome to the Ranch.

Here's a couple of tips to help you get the most out of the Ranch.

Firstly, please be sure to use code tags when posting code to the forums. Unformatted or unindented code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please click this link ⇒ UseCodeTags ⇐ for more information. Properly indented and formatted code greatly increases the probability that your question will get quicker, better answers.

I've gone ahead and added the code tags for you. See how much easier the code is to read?

Secondly, never just tell us "there are errors". Show us.

Cut and paste the exact error messages, and be sure to UseCodeTags for them too so that the formatting is preserved.


Kinda new here, still getting used to posting and such. Thanks for the advice!
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, first of all, your member variables should be private, not public. You have getters for them later in the class so there is no need to make them public. Making members public without good reason (and there is none here) is considered a poor practice.

Also, the class models a Car, so it should be Car, not Cars.

But primarily, you have three constructors with the same signature; and that just cannot be. If there are going to be multiple constructors, each signature must be unique.

For this class it looks like you really only need one constructor. But you have three because it looks like you are conflating constructors and constructing. Your constructor should be taking the values of the parameters and storing them in the member variables.

But your constructors are ignoring the parameters and assigning specific values to the members. This is wrong.

Specific instance of the class (such as the BMW, Suzuki, and Honda) should be created by other code that wants to create these instances, by calling the constructor via the new operator.

For example:


Your constructor should be something like:

Does that all make sense?
 
John Vorona
Ranch Hand
Posts: 48
Java Mac OS X Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh i see, everything makes sense to me besides this:



I am unsure of what does.

How can i call this.model = model; when a value has not been assigned to model yet?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You named the parameters the same as the member variables. So in the statement:

this.maker = maker;

this.maker is the member variable
maker is the parameter

That's a common way to do things, so no problem. Just understand the difference between using this. as a prefix, and leaving it off.

If you had named the first parameter abc instead of maker (and I am not advising you to do so -- it's just for illustration), the line would be:

this.maker = abc;
 
John Vorona
Ranch Hand
Posts: 48
Java Mac OS X Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahh I see, that makes much more sense now, and my program works correctly.
One final question, I am still fairly new to java and programming in general, in the code, what exactly does



do?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a non-static context, it is a reference to the instance.

In the case of the constructor, it is the newly constructed instance.
 
John Vorona
Ranch Hand
Posts: 48
Java Mac OS X Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once again, thank you for your time. I have a much clearer overview on the situation now !
 
John Vorona
Ranch Hand
Posts: 48
Java Mac OS X Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After playing around with the code i noticed that the constructor could also be written as


Just curious, why did you include this. before the variable, it seems more efficient to do so as above.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nazar Buko wrote:After playing around with the code i noticed that the constructor could also be written as ...


No, it cannot.

If all you are basing your assertion on is whether it compiles or not, that's only 5% of the job.

Does it actually work? (Hint: no, it does not.)

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nazar Buko wrote: it seems more efficient to do so


And, as a new developer, erase that phrase from your vocabulary immediately. Never do what "seems more efficient". That is the road to being a very poor developer. You do what is the most clear!

But in this case it's moot. Your version of the constructor will not work properly in the first place.
 
John Vorona
Ranch Hand
Posts: 48
Java Mac OS X Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you please explain why it will not work, for example this program works correctly when i compile and run it:



 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because this:


and this:

are NOT the same thing. Both cases have instance variables of maker, model, year, and price. But in one, you name the PARAMETERS the same thing. In the first example, you have TWO variables named "maker". You need to use "this." to identify one from the other. In the second example, it is obvious what maker refers to, since there is only one variable name that, so the "this." is not required.
 
John Vorona
Ranch Hand
Posts: 48
Java Mac OS X Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand what you are saying, although i have compiled and ran the updated version of the program where i have the variables and parameters as the same name and it worked correctly O.O.
 
John Vorona
Ranch Hand
Posts: 48
Java Mac OS X Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Update: Sorry, i recompiled and ran everything and i get the output; null, null, 0.
You are correct. Thanks for clarifying everything!
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly. When you do:

maker = maker;

You are assigning the value of the maker parameter to itself!

While this:

this.maker = maker;

assigns the value of the maker parameter to the maker instance variable.

This is why the this. is necessary in this case.

P.S. I always use this. to identify member variables even when it's not necessary. Not everyone does that; it's a matter of style. I do it because I find it brings clarity and consistency to the code, and helps to avoid inadvertent errors.
 
John Vorona
Ranch Hand
Posts: 48
Java Mac OS X Safari
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much, really helpful.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!