Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static member problem

 
Ramesh Sahu
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.
 
Gaurav Bhatia
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.
 
Raphael Rabadan
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.
 
Milan Sutaria
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic