|
-
September 29th, 1999, 06:45 AM
#5
Re: Member Class
Ashwani,
Leaving what you are trying to do apart, the concept is something like this - when u create a class that is a member of another class - it behaves exactly the same way as a variable does. Meaning, if u have a non-static variable in a class, u cannot reference it from inside a static context. Say, u hv 2 classes Outer and Inner, if class Inner is not static, it cannot be referenced from inside a static context of Outer. ( Check up the implicit - "this" - context topic. For ur code, the correction u've to do is simple -
import java.lang.* ;
public class Sample2
{
public static void main(String[] args)
{
f1();
}
class a1 { };
static void f1()
{
// Note : Function f1() is static. There is no "this" applicable here of class Sample2.
// An Inner class, if not static, needs an enclosing instance of the Outer class - or
// the function that does the instantiation should not be a static function.
Sample2 s = new Sample2();
a1 x1;
x1 = s.new a1();
// Now, if, in class a1, if u have referenced some variables belonging to class Sample2,
// they'll have a "this" context applicable to them ( s ).
}
}
Contact me if u have further clarifications.
Rgds,
R.Saravanan
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|