Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

what is my constructor doing?  RSS feed

 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys i have a class called Users



and another called OfficeComputer



now i understand what a constructor is,but what can mine do for me?
where would the "john paul" come into play...i just dont understand the practical application here
is my object John now have a variable named userName attached to the object
i cant seem to print out any variables from my main method...if i print out john i just get Users@140e19d
sorry i am very confused regarding this...also...method returns are also confusing me...but one step at a time...thanks for reading
 
L Hasan
Greenhorn
Posts: 24
4
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

There's a number of separate topics to be discussed here, but first an observation to your 'User' class:

You've declared and assigned your UserName variable within the constructor. This means you can't access this variable anywhere else, not even within the scope (a point of study) of your class, only your constructor knows about this.

A constructor is a class method that has no return type. It guarantees that a class is instantiated to a certain level of state, at the objects' creation.

The other issue you have is that you cannot print 'John' directly, as this only prints a value that represents the memory address of the object. The needs to have its method overridden (parent class being Object) to print anything meaningful. (another point of study)

As an aside, it might be better to step away from classes at the moment (even though everything in Java is built around this concept) and focus on writing methods to use within a main(), sort of like a mini-script. Try to understand the creation and scope of variables, how these can be passed into, and modified by methods. How a method can collect a group of commonly used statements and be used over and over with a single line of code.

I could go on, and I might based on responses.. =]

I hope that helps,

L.
 
Junilu Lacar
Sheriff
Posts: 11129
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
L Hasan wrote:A constructor is a class method that has no return type.

That is incorrect.  Yes, a constructor looks like a method that doesn't have a return type. However, constructors are not methods, they are not considered members of the class, and they are not inherited nor do they participate in polymorphism.

See these articles:
http://www.dummies.com/programming/java/how-to-use-a-constructor-in-java/
http://www.javaworld.com/article/2076204/core-java/understanding-constructors.html
 
Junilu Lacar
Sheriff
Posts: 11129
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja wrote:method returns are also confusing me...

Check out the Java Tutorials section on return values from methods: https://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html

If you have a question about returning a value from a method, please start a separate topic for it.
 
L Hasan
Greenhorn
Posts: 24
4
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
L Hasan wrote:A constructor is a class method that has no return type.

That is incorrect.  Yes, a constructor looks like a method that doesn't have a return type. However, constructors are not methods, they are not considered members of the class, and they are not inherited nor do they participate in polymorphism.

See these articles:
http://www.dummies.com/programming/java/how-to-use-a-constructor-in-java/
http://www.javaworld.com/article/2076204/core-java/understanding-constructors.html


Hi,

I'm currently studying the OCA Java 8 study guide pg. 17 (Jeanne Boyarsky and Scott Selikoff)
It's called a constructor, which is a special type of method... and there's no return type.
.

Is this one of those instances where it's one opinion versus another, and there's no definitive answer? It's very confusing when there are sources (online also) that contradict the ones posted.

Thanks,

L.
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi L Hasan

i do understand methods and have written many small programs that use methods...advice given to me on this forum was to start tackling OOP as soon as possible,so it does dishearten me
when you say i should step away from classes...i have learned a lot on my own over the past few months,but sometimes even after watching tutorials and reading books i find practical application
a problem...it would be nice to find some tutorials that actually show a practical application of how a lot of these techniques etc work on a real program,not just arbitrary x and y samples etc

thanks for reading,and thanks for the advice
 
Junilu Lacar
Sheriff
Posts: 11129
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
L Hasan wrote:
I'm currently studying the OCA Java 8 study guide pg. 17 (Jeanne Boyarsky and Scott Selikoff)
It's called a constructor, which is a special type of method... and there's no return type.
.

Is this one of those instances where it's one opinion versus another, and there's no definitive answer? It's very confusing when there are sources (online also) that contradict the ones posted.

Referring to the JLS, there's nothing in there that says a constructor is a method.  https://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.8
The JLS wrote:
In all other respects, the constructor declaration looks just like a method declaration that has no result (§8.4.5).

Constructor declarations are not members. They are never inherited and therefore are not subject to hiding or overriding.

It's been a while since I have taken the exam but if this were a question that came up in the exam:

Which of the following are true about Java constructors:
A. They are a special type of method
B. They are invoked by method invocation expressions
C. They are inherited
D. They are subject to hiding or overriding.
E. None of the above.

I would think the correct answer should be E

Also, I would think that because constructors are not members of a class, that would exclude them from being considered as methods since methods are members of the class.
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
also ..."You've declared and assigned your UserName variable within the constructor. This means you can't access this variable anywhere else, not even within the scope (a point of study) of your class, only your constructor knows about this." ---every tutorial and video i have seen does it this way
so where should they go instead? thanks
 
Junilu Lacar
Sheriff
Posts: 11129
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja wrote:also ..."You've declared and assigned your UserName variable within the constructor. This means you can't access this variable anywhere else, not even within the scope (a point of study) of your class, only your constructor knows about this." ---every tutorial and video i have seen does it this way

You are either looking at some very bad tutorials and videos or you did not understand what you saw/read. Please share some links to these tutorials and videos.

On line 4, you declare a local variable.  Constructors usually set instance variables, like this:

There's a big difference between this latter version and the former one.
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you're missing is this:

Without the first commented line, your variable is never in scope outside of the constructor.  Even better would be this:

Now you have better encapsulation.

[edit: Junilu posted while I was composing]
 
Liutauras Vilda
Marshal
Posts: 4633
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja,

Class Users, supposed to really read as User (singular) as object instance represents a singular user. Or your idea is different? Either way would be incorrect.
 
Junilu Lacar
Sheriff
Posts: 11129
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, class names are normally singular nouns, not plural.

When you instantiate an object of this class new User(), the code is grammatically correct because you are creating only ONE object.

You don't say "I'm making a chairs" when you're making only one chair or "She gave birth to a babies" when there was only one baby born. Likewise, to write new Users() is not grammatically consistent with the fact that you're really creating only ONE object that represents ONE user.

EDIT: Jinx! Liutauras and I seem to be on the same wavelength today.
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in the one tutorial off a link on this post there is this:



this doesnt work as firstName and lastName are underlined in red...why is this?
cause they not initialized? where should they be?
 
L Hasan
Greenhorn
Posts: 24
4
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In response to Junilu; I will take the advice of the JLS, the source is the most correct! Maybe this would be one to raise as an errata.

Not to derail the original thread. Don't be disheartened, I meant to get practice on things like variable scope (class, instance and local variables), when to declare and assign variables, working with arrays etc.

If you feel you are ready to tackle classes, I'll give you a clue as to what you need to do to fix your class as it stands. Read up on 'instance variables' and apply that to the UserName attribute. Take a look at 'method overriding', if you want to pass the object you instantiate to println(), you'll want to override toString() found in the 'Object class'.

Hope that helps,

L.
 
Junilu Lacar
Sheriff
Posts: 11129
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja wrote:
this doesnt work as firstName and lastName are underlined in red...why is this?
cause they not initialized? where should they be?

Because that code is only a snippet, it is not a complete example. The author assumes you know about instance variables and how to declare them in your class. We have already given you an example of this with the User class. Why don't you try to fix your code based on the example we gave you?
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jumilu,

thought as much...yes i know about instance variables,just didnt think a tutorial would be so lazy...thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!