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 / May 2006

Tip: Looking for answers? Try searching our database.

Constructor execution

Thread view: 
bagarow@yahoo.com - 25 May 2006 21:45 GMT
Hi

In the following code fragment:

class Cups {
   static int cupCount = 10;
   Cups () {
    System.out.println ("Cups");
               // Would like to create a array of cup objects here
   }
}

public class Cuppas2 {
   public static void main (String[] args) {
    System.out.println("Inside main");
    System.out.println("Cup Count is " + Cups.cupCount);
   }
}

The constructor for class Cups does not get executed when the static
field Cups.cupCount is referenced in the main method print statement.

If in the initialization of the class Cups, I wanted to use a non
simple code fragment e.g. creating an array of objects with a for loop,
is there a way to do that in the client code without calling new
Cuppas2()?

Thanks
Bob
Vova Reznik - 25 May 2006 21:53 GMT
> Hi
>
[quoted text clipped - 25 lines]
> Thanks
> Bob

use
static{
}
Eric Sosman - 25 May 2006 22:09 GMT
bagarow@yahoo.com wrote On 05/25/06 16:45,:
> Hi
>
[quoted text clipped - 5 lines]
>     System.out.println ("Cups");
>                 // Would like to create a array of cup objects here

   Go right ahead; nothing's stopping you.

>     }
> }
[quoted text clipped - 8 lines]
> The constructor for class Cups does not get executed when the static
> field Cups.cupCount is referenced in the main method print statement.

   No, of course not.  The job of a Cups constructor is
to initialize a newly-created Cups object.  You never ask
for any Cups objects to be created, so no Cups constructor
ever executes, or ever has any reason to execute.

> If in the initialization of the class Cups, I wanted to use a non
> simple code fragment e.g. creating an array of objects with a for loop,
> is there a way to do that in the client code without calling new
> Cuppas2()?

   Creating a new Cuppas2 object won't do anything about
Cups.  The Cuppas2 constructor (yes, there is one, even
though you can't see it) calls the no-argument constructor
of its superclass (Object, in this case) and then performs
any Cuppas2-specific initialization.  Since there isn't any
Cuppas2-specific initialization to perform, the constructor
just returns after the Object constructor returns to it.  At
no point does this do anything about the completely unrelated
class Cups: it doesn't make any new Cups objects, and in fact
it doesn't even cause the Cups class itself to be loaded.

   Perhaps (*perhaps*) what you're struggling toward is the
static initialization block, something like

    class Cups {
       static int cupCount = 10;

       static Cups[] cups = new Cups[cupCount];
       static {
           for (int i = 0;  i < cups.length;  ++i)
               cups[i] = new Cups();
       }

       Cups() {
           ... as before ...
       }
    }

... but I confess I'm not at all clear as to your intent.

Signature

Eric.Sosman@sun.com

bagarow@yahoo.com - 26 May 2006 01:11 GMT
> bagarow@yahoo.com wrote On 05/25/06 16:45,:
> > Hi
[quoted text clipped - 64 lines]
> --
> Eric.Sosman@sun.com

I had a typo in my original email. Instead of saying new Cuppas2(), I
should have said I wanted the initialization of class Cups to create a
pool of Cup objects without having to call new Cups() in the client
code but instead by the invocation of the Cups class via referencing
the static field Cups.cupCount . So the static block you all have
suggested in Cups does the trick.

Thanks much
Bob
bagarow@yahoo.com - 26 May 2006 01:11 GMT
> bagarow@yahoo.com wrote On 05/25/06 16:45,:
> > Hi
[quoted text clipped - 64 lines]
> --
> Eric.Sosman@sun.com

I had a typo in my original email. Instead of saying new Cuppas2(), I
should have said I wanted the initialization of class Cups to create a
pool of Cup objects without having to call new Cups() in the client
code but instead by the invocation of the Cups class via referencing
the static field Cups.cupCount . So the static block you all have
suggested in Cups does the trick.

Thanks much
Bob
Thomas Hawtin - 26 May 2006 10:06 GMT
>        static Cups[] cups = new Cups[cupCount];
>        static {
>            for (int i = 0;  i < cups.length;  ++i)
>                cups[i] = new Cups();
>        }

IIRC, Effective Java suggests doing something like:

    static Cups[] cups = createCups();

    private static Cups[] createCups() {
        Cups[] cups = new Cups[cupCount];
        for (int i=0; i<cups.length; ++i) {
            cups[i] = new Cups();
        }
        return cups;
    }

I prefer the former, but at least be aware of the possibility of the latter.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

salazar - 25 May 2006 22:17 GMT
bagarow@yahoo.com ha escrito:

> Hi
>
[quoted text clipped - 25 lines]
> Thanks
> Bob


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



©2008 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.