Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

instance of a class in a static context??

Thread view: 
dendeezen - 05 Apr 2007 09:04 GMT
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,
sfglbkf - 05 Apr 2007 10:14 GMT
> How can I create an instance of a class in a static context?
> I am a newbie, please point me the right direction.
[quoted text clipped - 21 lines]
> answer.
> Thanks,

Class1.c2 = c2;
tlas - 05 Apr 2007 13:32 GMT
[...]
>>public Class1 {
>>              // instance of another class
[quoted text clipped - 3 lines]
>>                          <code>
>>                }
[...]
> Class1.c2 = c2;

c2 isn't static so you can't do that.
tlas - 05 Apr 2007 13:41 GMT
> How can I create an instance of a class in a static context?
> I am a newbie, please point me the right direction.
[quoted text clipped - 20 lines]
> 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
dendeezen - 05 Apr 2007 14:12 GMT
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
Lew - 05 Apr 2007 14:30 GMT
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

tam@milkyway.gsfc.nasa.gov - 05 Apr 2007 14:28 GMT
> How can I create an instance of a class in a static context?
> I am a newbie, please point me the right direction.
[quoted text clipped - 21 lines]
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.