instance of a class in a static context??

D

dendeezen

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 {
// instance of another class
Class2 c2;
public Class1(Class2 c2) {
this.c2 = c2;
<code>
}
public static void main(String[] args, Class2 c2) {
//The next line is not allowed, I know , but what is
the solution?
this.c2 = c2;
Class1 c1 = new Class1(c2);
<code>
c1.setVisible(true);
}
}

I checked the books and surfed the net, but did not find the right
answer.
Thanks,
 
S

sfglbkf

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 {
// instance of another class
Class2 c2;
public Class1(Class2 c2) {
this.c2 = c2;
<code>
}
public static void main(String[] args, Class2 c2) {
//The next line is not allowed, I know , but what is
the solution?
this.c2 = c2;
Class1 c1 = new Class1(c2);
<code>
c1.setVisible(true);
}

}

I checked the books and surfed the net, but did not find the right
answer.
Thanks,

Class1.c2 = c2;
 
T

tlas

User said:
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 {
// instance of another class
Class2 c2; //[3]
public Class1(Class2 c2) {
this.c2 = c2; //[2]
<code>
}
public static void main(String[] args, Class2 c2) {
//The next line is not allowed, I know , but what is
the solution?
this.c2 = c2; //[1]
Class1 c1 = new Class1(c2);
<code>
c1.setVisible(true);
}
}

I checked the books and surfed the net, but did not find the right
answer.
Thanks,

I'm not sure what you're trying to achieve and what you mean by static
context.

In your case c2 ([3]) is not static so [1] is not necessary at all
because you initiate c2 in your constructor [2]

tlas
 
D

dendeezen

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 [...]
 
T

tlas

User said:
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 [...]

So all you have to do (and in fact you did that) is:

Class1 c1 = new Class1(c2);
c1.setVisible(true);

tlas
 
T

tam

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 {
// instance of another class
Class2 c2;
public Class1(Class2 c2) {
this.c2 = c2;
<code>
}
public static void main(String[] args, Class2 c2) {
//The next line is not allowed, I know , but what is
the solution?
this.c2 = c2;
Class1 c1 = new Class1(c2);
<code>
c1.setVisible(true);
}

}

I checked the books and surfed the net, but did not find the right
answer.
Thanks,

Your main function is static so it does not have access
to instance variables for that class... The question
you can ask yourself is what kind of variables does
it have access to. The answer is local variables and
class variables. You're already using a local
variable c1. So you can do either

Class Class1
static Class2 c2;
public static void main(String[] args) {
c2 = new Class2();
Class1 c1 = new Class1(c2);
...

Or
Class Class1

public static void main(String[] args) {
Class2 c2 = new C2();
Class1 c1 = new Class1(c2);
...

When you invoke the Class1 constructor [which
I didn't copy], it will link c2 appropriately within
c1. Unless there's something special going on, the
second alternative is likely what you want. You
were almost there with the code you poster but
didn't recognize that you can create c2 before
you associate it with c1.

Regards,
Tom McGlynn
 
L

Lew

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]
public Class1(Class2 c2) {
this.c2 = c2; //[2]
<code>
}
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.

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() +"\"." );
}
}
 
D

dendeezen

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,
 
L

Lew

dendeezen said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top