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
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.
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
Practice only makes habit, only perfect practice makes perfect.
Practice mindfully by doing the right things and doing things right.— Junilu
[How to Ask Questions] [How to Answer Questions]
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.
Practice only makes habit, only perfect practice makes perfect.
Practice mindfully by doing the right things and doing things right.— Junilu
[How to Ask Questions] [How to Answer Questions]
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.
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
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.
Practice only makes habit, only perfect practice makes perfect.
Practice mindfully by doing the right things and doing things right.— Junilu
[How to Ask Questions] [How to Answer Questions]
so where should they go instead? thanks
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.
Practice only makes habit, only perfect practice makes perfect.
Practice mindfully by doing the right things and doing things right.— Junilu
[How to Ask Questions] [How to Answer Questions]
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]
All things are lawful, but not all things are profitable.
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.

Practice only makes habit, only perfect practice makes perfect.
Practice mindfully by doing the right things and doing things right.— Junilu
[How to Ask Questions] [How to Answer Questions]
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.
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?
Practice only makes habit, only perfect practice makes perfect.
Practice mindfully by doing the right things and doing things right.— Junilu
[How to Ask Questions] [How to Answer Questions]