aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static member problem 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 "Static member problem" Watch "Static member problem" New topic
Author

Static member problem

Ramesh Sahu
Ranch Hand

Joined: Jul 14, 2008
Posts: 32
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

Joined: Jan 01, 2007
Posts: 49
"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.


~Gaurav<br />SCJP5
Ramesh Sahu
Ranch Hand

Joined: Jul 14, 2008
Posts: 32
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

Joined: Jul 05, 2008
Posts: 141
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.


SCJP Java 6 (98%) - Story, SCJA (88%) - Story
Milan Sutaria
Ranch Hand

Joined: Jul 10, 2008
Posts: 121
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!


SCJP 6 83%
Working on SCWCD/OCPJWCD
Ramesh Sahu
Ranch Hand

Joined: Jul 14, 2008
Posts: 32
Thanks a lot
I got that
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static member problem