| Author |
what's wrong with this code?
|
midhuna peru
Ranch Hand
Joined: Aug 24, 2012
Posts: 48
|
|
hi guys!
I was experimenting with protected members. There seems to be silly mistake in my code but I can't figure out what's wrong. here's the code
now the error is "<identifier> expected" at line 12. What's the mistake here?
|
 |
Nikhil Sagar
Ranch Hand
Joined: Apr 21, 2012
Posts: 214
|
|
No, i am sure you are getting error on line 9 not on 12.
at this line..
Now think about it, Why are you getting error here on this line .
|
OCPJP 6 86%
|
 |
midhuna peru
Ranch Hand
Joined: Aug 24, 2012
Posts: 48
|
|
|
yah the same line. No m not getting it. must be silly mistake.
|
 |
midhuna peru
Ranch Hand
Joined: Aug 24, 2012
Posts: 48
|
|
|
no idea
|
 |
Nikhil Sagar
Ranch Hand
Joined: Apr 21, 2012
Posts: 214
|
|
midhuna peru wrote:yah the same line. No m not getting it. must be silly mistake.
Okay, to make it interesting and more understandable to you i am not giving you the correct answer but i am going to ask you some questions, answer me and you will find it out by your self okay.
Tell me the name of the place where you are getting this error ?
|
 |
midhuna peru
Ranch Hand
Joined: Aug 24, 2012
Posts: 48
|
|
line 11
t1.no=10;
|
 |
Nikhil Sagar
Ranch Hand
Joined: Apr 21, 2012
Posts: 214
|
|
midhuna peru wrote:line 11
t1.no=10;
No no no...
tell me in java what we call the place where line 9 is written in your source file ??
|
 |
midhuna peru
Ranch Hand
Joined: Aug 24, 2012
Posts: 48
|
|
|
class Test2
|
 |
Nikhil Sagar
Ranch Hand
Joined: Apr 21, 2012
Posts: 214
|
|
No, we call that place- is a place where we declare static or non-static variables.
Addition- If it is a non-static then it will go into the object of enclosing class. in your case it will go into the Test2 class's object.
|
 |
midhuna peru
Ranch Hand
Joined: Aug 24, 2012
Posts: 48
|
|
|
now it is instance variable of Test1. But I am not getting your question.
|
 |
midhuna peru
Ranch Hand
Joined: Aug 24, 2012
Posts: 48
|
|
tell what it is
|
 |
Nikhil Sagar
Ranch Hand
Joined: Apr 21, 2012
Posts: 214
|
|
okay, so if my last post is correct then
will go into the Test2 class's object which does not make any sense. that is why compiler is complaining.
|
 |
midhuna peru
Ranch Hand
Joined: Aug 24, 2012
Posts: 48
|
|
|
I din get you. An object for Test1 is created in Test2 class. Memory will be created for Test1 class's object right.
|
 |
midhuna peru
Ranch Hand
Joined: Aug 24, 2012
Posts: 48
|
|
|
got it. got it. i initialised object for wrong class! i was trying out inheritance and I did not edit properly.....thanks
|
 |
Nikhil Sagar
Ranch Hand
Joined: Apr 21, 2012
Posts: 214
|
|
midhuna peru wrote:I din get you. An object for Test1 is created in Test2 class. Memory will be created for Test1 class's object right.
have a look on this code...
now at line 3,4 and 5 you are declaring instance variables for Peru class . Got it ??
|
 |
Nikhil Sagar
Ranch Hand
Joined: Apr 21, 2012
Posts: 214
|
|
midhuna peru wrote:got it. got it. i initialised object for wrong class! i was trying out inheritance and I did not edit properly.....thanks
No no no, you are doing a thing at wrong place. And there is one more wrong thing you are doing there in Test2 class but first thing first.
|
 |
gurpeet singh
Ranch Hand
Joined: Apr 04, 2012
Posts: 867
|
|
|
you cannot have executable code inside the class but outside any methods . you are assigning t1.no = 10; which must be put in method/initializer rather than writing it inside the class.
|
OCPJP 6(100 %) OCEWCD 6(91 %)
|
 |
midhuna peru
Ranch Hand
Joined: Aug 24, 2012
Posts: 48
|
|
i corrected line 24. it should be
still the same error.
|
 |
Nikhil Sagar
Ranch Hand
Joined: Apr 21, 2012
Posts: 214
|
|
gurpeet singh wrote:you cannot have executable code inside the class but outside any methods . you are assigning t1.no = 10; which must be put in method/initializer rather than writing it inside the class.
Gurpreet, you spoiled the suspense
|
 |
midhuna peru
Ranch Hand
Joined: Aug 24, 2012
Posts: 48
|
|
|
oh right right. Except declaration everything should be inside method. silly of me....i knew it will be stupid mistake. so much for changing a code. next time i should type code fresh if changes are major. Thanks so much.
|
 |
Nikhil Sagar
Ranch Hand
Joined: Apr 21, 2012
Posts: 214
|
|
midhuna peru wrote:oh right right. Except declaration everything should be inside method. silly of me....i knew it will be stupid mistake. so much for changing a code. next time i should type code fresh if changes are major. Thanks so much.
Ah i am glad you got it,
|
 |
midhuna peru
Ranch Hand
Joined: Aug 24, 2012
Posts: 48
|
|
|
Thanks Gurpreet and Nikhil. i din get it until i typed the code fresh. then i saw difference.
|
 |
Nikhil Sagar
Ranch Hand
Joined: Apr 21, 2012
Posts: 214
|
|
midhuna peru wrote:Thanks Gurpreet Nikhil. i din get it until i typed the code fresh. then i saw difference.
No need to say thanks, we are all here to share our knowledge.
|
 |
Nikhil Sagar
Ranch Hand
Joined: Apr 21, 2012
Posts: 214
|
|
midhuna, one more thing i noticed that can lead to trouble that always use protected members of the super class through inheritance instead of by using association.
for example.
will not work.
|
 |
midhuna peru
Ranch Hand
Joined: Aug 24, 2012
Posts: 48
|
|
hey! this is what my code was first. Ofcourse Test1 was not public I just copied and edited in new file and ended up with trouble. When I Typed code fresh i did it right . only on comparing edited code and fresh code i understood the mistake.
Anyway whats wrong with Base Class being Public?
|
 |
 |
|
|
subject: what's wrong with this code?
|
|
|