Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
The only way to access the instance variable while you are in the scope of the local variable by the same name is by using the keyword "this".
Always Belive On Logic!!
Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
SCWCD: Online Course, 50,000+ words and 200+ questions
http://www.examulator.com/moodle/course/view.php?id=5&topic=all
Always Belive On Logic!!
Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
1)In RaVISH's reply, the shadowing part:we can't reinitialize a variable inside a class.but in method it is possible;
class Class{int i=5;i=10;//--->compilaton error
method(){int j = 5;j=10;}}
How this is?
2)If I put anything under '{}'block it will be considered as local in a "method".Why I am telling this is in an "initializer block "({})if I put any declarations it will be local only.But if I put any declarations it will be local only.But if I give values that is declared outside block it will be valid outside block also(then called initialization).
How this 'sitting in two boats at same time' possible for compiler to '{}'.
NB:I am not from hard programming.But I wish to know this clearly.
"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Originally posted by jyothi abraham:
I will put my qusetions once again,now with examples;
(Now I am not asking about shadowing,simply reinitialization)
1)(Now I am not asking about shadowing,simply reinitialization)
<pre>
class Class{
int i=5;
//i=10;--->compilaton error if I add this part."why?"
method(){
int j = 5;
j=10;//inside method I can put same thing no errors. "why no errors"
}
}
</pre>
How this is?why?
Originally posted by jyothi abraham:
2)If I put anything under '{}'block it will be considered as local in a "method".
Why I am telling this is in an "initializer block "({})if I put any declarations it will be local only.But if I put any declarations it will be local only.But if I give values that is declared outside block it will be valid outside block also(then called initialization).
How this 'sitting in two boats at same time' possible for compiler to
'{}'.
eg:a)
<PRE>
class Doubt1//program started.
{
int a;
{
a=10;
int b = 5;
}
System.out.println(a);//I get a = 10.Then why can't I acess 'b'as
// shown below?
//System.out.println(b);--->error,I can't access b from here.This
//isjust like a method variable you can't access.
void method()
{
int c = 11;
}
//System.out.println(c);--->just like the case of 'b'.
}//program finished.
<PRE>
WHAT IS NOT POSSIBLE IN METHOD BLOCK IS POSSIBLE IN INITIALIZER BLOCK.
WHY?
"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
Hey, sticks and stones baby. And maybe a wee mention of my stuff:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
|