Click to See Complete Forum and Search --> : Memeber Class


ashwani singhal
September 29th, 1999, 05:24 AM
I have written following code. JDK is giving some error message. I am unable to correct it.
Help me.

Code is

import java.lang.* ;


public class Sample2
{

public static void main(String[] args)
{
f1();
}
class a1 { };

static void f1()
{
a1 x1;
x1 = new a1();
}

}

Compiler Error :
No enclosing instance of class Sample2 is in scope; an explicit
one must be provided when creating inner class Sample2. a1, as in "outer. new I nner()" or "outer. super()".
x1 = new a1();

Erbeere
September 29th, 1999, 06:19 AM
Hi

Ähmmmm what do you try with this ?
If you tell me what you wanna do, than I can help you but that is total false.
At first:
Every class is it own file
So if you wanna make an instance of the class A1 than it must be a own file
but I don´t understand what to the hell that program should do ?!?

ashwani singhal
September 29th, 1999, 06:37 AM
I have a declare a member class in main class.
Now i want to create a instance of that member class in the main class , is it possible ,
on which places i can create the instance of that member class

Erbeere
September 29th, 1999, 06:42 AM
OK
If you want to create an instance of a class, than you need at first another class. So if you want to try it, than you can to it like that:
Make a File named Test1.java
in that one you just write
public class Test1
{
int Number1;
int Number2;
}

than you create a second file named Test2.java
there you write:
public class Test2
{
public static void main (String args[])
{
Test1 a = new Test1();
a.Number1 = 5;
}
}

So you have created an instance of the class Test1 named a. Every class must be a own file ! ! !

R.Saravanan
September 29th, 1999, 06:45 AM
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

poochi
September 29th, 1999, 12:28 PM
> So if you wanna make an instance of the class A1 than it must be a own file

Erbeere, Have you heard about inner classes ? She is trying to learn something ..
Who said that every class has it's own file ? We can put 'n' number of classes inside
a single java file. BUT YOU CANT PUT MORE THAN ONE "PUBLIC" CLASS INSIDE A JAVA FILE.

> but that is total false

That's why she has posted this question. Otherwise she wont...

I agree with Saravanan.

One more Solution is , make the al class as a static variable in your sample2 class...


import java.lang.* ;


public class Sample2
{

public static void main(String[] args)
{
f1();
}
public static class a1 { };

static void f1()
{
a1 x1;
x1 = new a1();
}

}




Poochi..