aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes local class in a static method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "local class in a static method" Watch "local class in a static method" New topic
Author

local class in a static method

basha khan
Ranch Hand

Joined: Jan 26, 2002
Posts: 516
look the code ,
--------------------------------------------------
class localclassTest{
int a=10;
public static void main(String asd[]){
final int b=20;//(a)
class inner{
int a=30;
void print(){
int a=40;
System.out.println(a);//(1) local varable
System.out.println(this.a);// (2)inner class member variable
//System.out.println(t3.this.a);//(3) outer class member variable
}
}
new inner().print();
}}
---------------------------
what is wrong in //(3).why outer class member not availeable?
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
System.out.println(t3.this.a);//(3) outer class member variable
I think t3 => localclassTest
then as 'a'(member var) is not static ..
do not confuse with inner class.. it is simple question which says that U CAN NOT access any NON-static member from static method.
CMIW
HTH


"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
Shivaji Marathe
Ranch Hand

Joined: Jan 11, 2002
Posts: 203
You are creating the inner class in a static method and within that inner class you are trying to access a non static member of the outer class without creating an instance of the outer class.
You need to create t3 as an instance of the outer class, before you refer to it .

[ January 31, 2002: Message edited by: Shivaji Marathe ]
Paul Salerno
Ranch Hand

Joined: Jan 17, 2002
Posts: 172
Sivaji
System.out.println(t3.a);//(3) outer class member variable
I was under the im impression that you can't access a non-static variable within a static method main(), since the inner class and its method is within a static method main()
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
You're not accessing a non-static variable.
t3 is declared local to the method:
Foo t3= new Foo() ; // **** create t3 ****
System.out.println(t3.a);//(3) outer class member variable
You're accessing a member of a class through a local reference. This is perfectly legal.
Rob
[ January 31, 2002: Message edited by: Rob Ross ]

Rob
SCJP 1.4
 
Consider Paul's rocket mass heater.
 
subject: local class in a static method