Forums Register Login

what's wrong with this code?

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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 .
+Pie Number of slices to send: Send
yah the same line. No m not getting it. must be silly mistake.
+Pie Number of slices to send: Send
no idea
+Pie Number of slices to send: Send
 

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 ?
+Pie Number of slices to send: Send
line 11
t1.no=10;
+Pie Number of slices to send: Send
 

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 ??
+Pie Number of slices to send: Send
class Test2
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
now it is instance variable of Test1. But I am not getting your question.
+Pie Number of slices to send: Send
tell what it is
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
I din get you. An object for Test1 is created in Test2 class. Memory will be created for Test1 class's object right.
+Pie Number of slices to send: Send
got it. got it. i initialised object for wrong class! i was trying out inheritance and I did not edit properly.....thanks
+Pie Number of slices to send: Send
 

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 ??
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
i corrected line 24. it should be

still the same error.
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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,
+Pie Number of slices to send: Send
Thanks Gurpreet and Nikhil. i din get it until i typed the code fresh. then i saw difference.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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?
You save more money with a clothesline than dozens of light bulb purchases. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1063 times.
Similar Threads
I am getting clone() has protected access in java.lang.Object
.equals() and "==" what is the output of the following code and why?
Class Reference
cloning
could someone help?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 09:43:55.