File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes class question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "class question" Watch "class question" New topic
Author

class question

Kartik Mahadevan
Ranch Hand

Joined: Feb 16, 2005
Posts: 47
Sir
What do we mean by "loading a class" ?
I am asking this bcos I read
Class dog
{
Static ()
static int variable d
{
System.out.println("Dog")
}
}

MAIN FUNCTION()
{
create a dog refrence variable a
and Dog is printed
}
So i had the doubt abt difference b/n creating an object and loading a class. Does loading a class mean that I am loading it in a memory location and of what use it is?
In the above question does the memory allocation for static variable d and execution of Static() take place just after creating a dog refrence variable or after an object is created associated with this refrence variable ?
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Any program that runs needs to be loaded into memory. This is the same whether you are talking about Windows itself or other programs like a word processor, spreadsheet, or computer game. The computer can only run the program after it is loaded in memory. When you run a Java program, the Operating System must first start the Java Virtual Machine (JVM). Before any class is used, it must be loaded into memory. At this time, the JVM will create any static variables and run any static initializers.

However, the program you gave will not compile. I realize that some portions are proably meant to be pseudocode. However, the parts that you have questions about have incrrect syntax. So let me make some minor modifications. I will also flesh out the pseudocode to illustrate some other issues. I haven't tried to compile it, but I'm fairly confident that it will work as is.



What do you think the output will be? If it surprises you, let us know what you think is happening. We'll be glad to help clarify.

Layne


Java API Documentation
The Java Tutorial
Kartik Mahadevan
Ranch Hand

Joined: Feb 16, 2005
Posts: 47
sir
The output i think will be


Starting main
dog
another dog
ending main


but what is the meaning of
{
System.out.println("Another dog ")

}
so what I understand is that whenever a class refrence variable is declared
the class is loaded into memory and static things are executed
Right?
Kartik Mahadevan
Ranch Hand

Joined: Feb 16, 2005
Posts: 47
sir
Is
{
System.out.println("daadaaad");
}

some kind of constructor which get's executedafter new is executed?
Kartik Mahadevan
Ranch Hand

Joined: Feb 16, 2005
Posts: 47
Sir
I think

Dog
Starting main()
Dog
Another Dog
Ending main()

can also be an answer bcos during the first class loading static{ }
will get executed
next first S.O.P{"starting main()") will get executed
next staic will again executed bcos creating Dog d amounts to loading the class again.next another dog will get executed after the formation of dog object.
Next Ending main() will get executed.

Please correct me if I am wrong.

Thanks
Kartik
Marco Vanoli
Ranch Hand

Joined: Jan 12, 2005
Posts: 99
Originally posted by Kartik Mahadevan:

{
System.out.println("daadaaad");
}



But constructor arn' t calssname(){..} ???


bye, <br />Marco
Hentay Duke
Ranch Hand

Joined: Oct 27, 2004
Posts: 198
Constructors are named the same as the class, so in this case a constructor would be named Dog and have no return value. Something like this


When the class is loaded the static blocks are executed. They aren't executed again after that.

Have you tried running the program to see what the output is?
[ September 19, 2005: Message edited by: Hentay Duke ]
Kartik Mahadevan
Ranch Hand

Joined: Feb 16, 2005
Posts: 47
Sir
Could someone plz give me the answer to the problem.In this example When is the class being loaded?Is it loaded when we use Java command or is it being loaded after the refrence to the variable Dog is being made? or is it being loaded at both these times (during java Dog and Dog d ;which means that static block would be executed twice.Also if I have a statement Dog d is the class dog being loaded?
Thanks
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
It is an initialiser, and is executed just before a constructor is executed. It is a non-static context. It is typically used for assignmnt of final fields that are not-static, non-constants (JLS 15.28), or when those fields are assigned inline, the compiler simply creates the initialiser. For static, non-constant fields, a static initializer is created. A static initialiser (one marked with the static keyword) is executed at class load time.


Tony Morris
Java Q&A (FAQ, Trivia)
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Kartik Mahadevan:
...
but what is the meaning of
{
System.out.println("Another dog ")

}


This is called an initializer block. It is NOT a constructor because the constructor MUST have a name that matches the class name.


so what I understand is that whenever a class refrence variable is declared
the class is loaded into memory and static things are executed
Right?


This is close. However, the "static things" (or more formally, the static initializers) are only called ONCE when the class is first used. This may either be through declaring a reference variable, creating a new object using new, or calling a static method with the class name (as well as other reasons that are more advanced than I want to get into).
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Kartik Mahadevan:
Sir
I think

Dog
Starting main()
Dog
Another Dog
Ending main()

can also be an answer bcos during the first class loading static{ }
will get executed
next first S.O.P{"starting main()") will get executed
next staic will again executed bcos creating Dog d amounts to loading the class again.next another dog will get executed after the formation of dog object.
Next Ending main() will get executed.

Please correct me if I am wrong.

Thanks
Kartik


There is only one correct answer. This one is a bit closer than your first attempt. I suggest you compile it yourself and see what happens. If you don't understand the output, see my post above for a clue. If you still have trouble, please let us know and I will explain further.

Layne
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
You should play with this some more by compiling it yourself. There are some other variations you can add:

1) Add a constructor like the one Hentay-Duke gave above and see how where it fits.
2) Create another Dog object either with a new reference variable or using the one that you already have.
3) Create another class, say Cat, that has a constructor, initializer block, and static initializer. Then create instances of Cat as well.

I think we have given you enough information here to help you understand the answer. You should compile the code I gave above and run it yourself to see what it does. This is the best way to get "the answer", in my opinion. However, if you don't understand why the output is the way it is, then feel free to ask more questions about it.

Layne
Megha Jain
Ranch Hand

Joined: Sep 12, 2005
Posts: 30
Hi, i tried this code:........
class Dog{
static int d;
public Dog(){
System.out.println("Constructor");
}
static {
System.out.println("Dog");
}
{
System.out.println("Another Dog");
}
public static void main(String[] args) {
System.out.println("Starting main()");
Dog d = new Dog();
System.out.println("Ending main()"); }
}

Output: Dog
stating main
Another dog
Constructor
Ending main

Query: i didn't understood why i got Another Dog begore Constructor........
I read all the replies above in that i read one line..... Initializer gets ececuted before conStructor..... but how can System.out.println be intializer..... so was it related to this only or something else........
Anyways tell me why it happened that i got Another Dog before Constructor.

Thanks
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061


This is called an initializer. The S.O.P itself is NOT an initializer. The { and } indicate that it is an initializer. All non-staic initializers are executed each time just before the constructor.

Compare it to this:


This is a static initializer which is only executed once when the class is loaded.

Does this help clarify what is happening?

Layne
Megha Jain
Ranch Hand

Joined: Sep 12, 2005
Posts: 30
Thank u so much.......... Today i learned one more thing.
Sachin Ramesh Vir
Greenhorn

Joined: Sep 15, 2005
Posts: 23

The { and } indicate that it is an initializer. All non-staic initializers are executed each time just before the constructor.




Output:
Dog-static initializer
Starting main()
Another Dog1- non-static initializer
Constructor
Another Dog2-non-static initializer
Ending main()

In the above program after executing the constructor only, the non-static initializer get executed. Why?
Kenneth Albertson
Ranch Hand

Joined: Sep 18, 2005
Posts: 59

Output:
Dog-static initializer
Starting main()
Another Dog1- non-static initializer
Constructor
Another Dog2-non-static initializer
Ending main()

In the above program after executing the constructor only, the [Dog2] non-static initializer get executed. Why?


The Java Language Specification, section 8.1.6 Class Body and Member Declarations, says (in part):

ClassBodyDeclaration:
ClassMemberDeclaration
InstanceInitializer
StaticInitializer
ConstructorDeclaration
ClassMemberDeclaration:
FieldDeclaration
MethodDeclaration
ClassDeclaration
InterfaceDeclaration
Translated into everyday language, this means that a Class Body can contain Member Declarations (members are fields, methods, classes and interfaces), Instance Initializers, Static Initializers, or Constructor Declarations. Also, because they are not mentioned anywhere else, this is the only place that Instance Initializers or Static Initializers can appear.

Here is another way of conveying the same information, this time from Arnold, Gosling & Holmes, The Java Programming Language, 3rd edn, section 2.5.2 Initialization Blocks: "An initialization block is a block of statements that appears within the class declaration, outside of any member, or constructor, declaration ...".

So, is the block containing the "Dog2" statement an instance initializer ?

Does that answer your question of "why was the Dog2 statement executed after the constructor" ?

By the way, "Instance Initializer" is a better name than "non-static initializer" for this thing we are talking about, because it is less likely to be confused with "field initializer", which is another type of non-static initializer.

Hope that helps.
[ September 20, 2005: Message edited by: Kym Thompson ]
Marco Vanoli
Ranch Hand

Joined: Jan 12, 2005
Posts: 99
Originally posted by Layne Lund:

... This may either be through declaring a reference variable....


I tryed to make some experience tryng to play with this class and i have a doubt about your assertions.. look at my code where declaring a reference variable does not static stuff..




output:


[ September 27, 2005: Message edited by: Marco Vanoli ]
[ September 27, 2005: Message edited by: Marco Vanoli ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: class question