• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

local class in a static method

 
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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()
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic