wClass1.c2 = c2;
c2 isn't static so you can't do that.
right, it doesn't work.
public static void main [...] , this is what I mean by a static
context.
I try to set visible Class1.
The more complete line is code is:
public Class1 extends JFrame [...]
tlas - 05 Apr 2007 14:20 GMT
> wClass1.c2 = c2;
> c2 isn't static so you can't do that.
[quoted text clipped - 9 lines]
>
> public Class1 extends JFrame [...]
So all you have to do (and in fact you did that) is:
Class1 c1 = new Class1(c2);
c1.setVisible(true);
tlas
dendeezen wrote:
>> How can I create an instance of a class in a static context?
>> I am a newbie, please point me the right direction.
>> My code looks like:
>>
>> public Class1 {
Not a very good class name. java.lang.Class already exists, and your class is
not similar to it.
Try a name like "Example1".
You forgot the keyword 'class'.
>> // instance of another class
>> Class2 c2; //[3]
[quoted text clipped - 3 lines]
>> }
>> public static void main(String[] args, Class2 c2) {
This is not the signature for main(). The "real" main() only has one
argument. You will not be able to run this class from the command line.
Go ahead and create a test instance of Example2 inside your main() method
instead of trying to pass it in.
>> //The next line is not allowed, I know , but what is
>> the solution?
>> this.c2 = c2; //[1]
That's because 'this' doesn't exist in a static context. You have to create
an instance of Example1.
>> Class1 c1 = new Class1(c2);
>> <code>
>> c1.setVisible(true);
Now do not write GUI code at this stage, it only complicates things. There is
no 'setVisible()' because there is no GUI, so it will fail to compile.
>> }
>> }
You have to create an instance of the class to reference anything not static.
********
class Example2
{
public String getName()
{
return "example 2";
}
}
public class Example1
{
private Example2 eg;
public Example1( Example2 e2 )
{
this.eg = e2;
}
public final Example2 getEg()
{
return eg;
}
public static void main( String [] args )
{
Example2 e2 = new Example2();
Example1 e1 = new Example1( e2 );
System.out.println( "Example1 has an Example2 named \""+
e1.getEg().getName() +"\"." );
}
}

Signature
Lew
dendeezen - 05 Apr 2007 16:22 GMT
Lew,
The public class Class1 alias Example1 worked fine until I tried to
intantiate Class2 alias Example2.
I still don't succeed to get my 'real' code running, ... but I keep on
trying..
regards,
Lew - 05 Apr 2007 22:37 GMT
> The public class Class1 alias Example1 worked fine until I tried to
> intantiate Class2 alias Example2.
>
> I still don't succeed to get my 'real' code running, ... but I keep on
> trying..
What code did you use to instantiate Example2?
Did it look like this:
Example2 e2 = new Example2();
?
If not, that could be your problem. The code you posted had the Class2 passed
in as an argument to main(), which would not work. This was mentioned to you.
You aren't still doing that, are you?
What exactly are you doing? If you provide an SSCCE we can help. If you
insist on keeping your problem a secret there's nothing we can do to help you.
Provide an SSCCE.

Signature
Lew