• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Static member problem

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.util.*;
public class testing1 {

class Inner{
String s;
Inner(String s){
this.s = s;
}
public String toString(){
return s;
}
}
public static void main(String... args)
{
List<Inner>l = new ArrayList<Inner>();
l.add(new Inner("I"));
l.add(new Inner("AM"));
for(Inner p:l){
System.out.println(p);
}
}

}

When I compile the above program,
It shows compilation error
testing1.java:16: non-static variable this cannot be referenced from a static context
l.add(new Inner("I"));
^
testing1.java:17: non-static variable this cannot be referenced from a static context
l.add(new Inner("AM"));

My problem is what does "this" refer to in the above two lines ?

But when I modify my program as ->

import java.util.*;
public class testing1 {
int x;
class Inner{
String s;
Inner(String s){
this.s = s;
}
public String toString(){
return s;
}
}
public static void main(String... args)
{
List<Inner>l = new ArrayList<Inner>();
l.add(new testing1().new Inner("I"));
l.add(new testing1().new Inner("AM"));
for(Inner p:l){

}
}

}

It compiles . Why?
Can anyone help me.
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"this" here refers to the reference of the class testing1.
This is happening because you are trying to create an instance of the inner class directly which is NOT allowed.
 
Ramesh Sahu
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes , I got that.
But why does the following code compile ,

import java.util.*;
class testing1 {

class Inner{
String s;
Inner(String s){
this.s = s;
}
public String toString(){
return s;
}
}
public static void main(String... args)
{

}



void meth(){
List<Inner>l = new ArrayList<Inner>();
l.add(new Inner("I"));
l.add(new Inner("AM"));
for(Inner p:l){
System.out.println(p);
}
}
}

even though I have tried to create the inner class directly?
I know that non static references cannot be referenced from a static context. I am bit confused when it comes to an inner class.
Please help me out.
 
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Ramesh Sahu:
Yes , I got that.
But why does the following code compile ,

import java.util.*;
class testing1 {

class Inner{
String s;
Inner(String s){
this.s = s;
}
public String toString(){
return s;
}
}
public static void main(String... args)
{

}



void meth(){
List<Inner>l = new ArrayList<Inner>();
l.add(new Inner("I"));
l.add(new Inner("AM"));
for(Inner p:l){
System.out.println(p);
}
}
}

even though I have tried to create the inner class directly?
I know that non static references cannot be referenced from a static context. I am bit confused when it comes to an inner class.
Please help me out.



It compiles because you are creating Inner's by an instance method of testing1, so you already have a reference of testing1, this.
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramesh, what Raphael explained is correct ... you will access the method meth() with an instance of the testing1 i.e
testing1 te = new testing1();
te.meth();
so inside meth() you will always have this reference. But thats not the case for static method! & the inner class(not a toplevel class) always requires the outer class refernce!
 
Ramesh Sahu
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot
I got that
 
Why should I lose weight? They make bigger overalls. And they sure don't make overalls for tiny ads:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic