Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 132,172 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,559 people online right now. Registration is fast and FREE... Join Now!




what is it ?

 
Reply to this topicStart new topic

what is it ?

liem_nguyenho
post 11 Oct, 2008 - 01:54 AM
Post #1


New D.I.C Head

*
Joined: 23 Aug, 2007
Posts: 14


My Contributions


I searched through the Internet and I see there is a new declaration, but i don't know what it is. Okie, take a look at this code:
CODE


public class testingclass {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        NewThing hbu = new NewThing();
    }

}

class NewThing {
     ////new
    {
        System.out.println("this is a new thing.");
    }
    ////new

    NewThing() {
        System.out.println("this is my constructor.");
    }
}



When i run testtingclass, "this is a new thing" is printed before "this is my constructor". Can anybody explain it to me ?
Thanks in advance.
User is offlineProfile CardPM

Go to the top of the page

abgorn
post 11 Oct, 2008 - 02:46 AM
Post #2


Hello Crap for Brains

Group Icon
Joined: 5 Jun, 2008
Posts: 858



Thanked 4 times

Dream Kudos: 25
My Contributions


This is because the main says to start NewThing() which has System.out.println("this is my constructor") in it. It won't run the other System.out.println because it's not in the method NewThing().

Or that's what I think you mean, what exactly is the new declaration?
User is online!Profile CardPM

Go to the top of the page

JeroenFM
post 11 Oct, 2008 - 03:01 AM
Post #3


D.I.C Head

Group Icon
Joined: 30 Jun, 2008
Posts: 182



Thanked 9 times

Dream Kudos: 100
My Contributions


It's called an initializer block.
User is offlineProfile CardPM

Go to the top of the page

pbl
post 11 Oct, 2008 - 02:22 PM
Post #4


D.I.C Lover

Group Icon
Joined: 6 Mar, 2008
Posts: 2,946



Thanked 187 times

Dream Kudos: 75
My Contributions


QUOTE(JeroenFM @ 11 Oct, 2008 - 04:01 AM) *

It's called an initializer block.


JeroenFM is right and it does not need to be ibetwenn {}

CODE

class NewThing {

        System.out.println("this is a new thing.");

    NewThing() {
        System.out.println("this is my constructor.");
    }
}


You are probably more used to constructs like:

CODE

class NewThing {

    int i = 0;
    double Pi = 3.14159;
    Rectangle rect = new Rect();
    System.out.println("this is a new thing.");

    NewThing() {
        System.out.println("this is my constructor.");
    }
}


All the code between the class declaration and the constructor is executed before the constructor is called but for static block initializer that are executed only once
User is online!Profile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/21/08 03:12PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month