File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Why constructor is not called. ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why constructor is not called. ?" Watch "Why constructor is not called. ?" New topic

Why constructor is not called. ?

Ambika Jain
Ranch Hand

Joined: Jan 27, 2005
Posts: 48
When we call the "public static void main(String args[]) " method, why is the constructor not called. Is it not initializing the class.

When I execute this class: It does intialize the variable i and the static block. Why is the constructor not being called ? Why does constructor get called only when we create an instance using "new". In the program below is "Allgo" object not getting instantiated when the main() is getting called ?

public class Allgo {
static int i=6 ;
static {

public static void main(String args[])

Thanks in advance

Ambika Jain
Deepak Bala

Joined: Feb 24, 2006
Posts: 6662

Some clues - think about why the main method is public and static. How would the JVM call your program ?

SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15092

Make sure you understand the difference between an object and a class.

A class is a "blueprint" which is used to make objects. An object is an instance of a class. A constructor does not initialize a class. It initializes an object.

Do you know exactly what static means? It means that something is class-wide; for example, if you have a static member variable, then there is just one variable which is shared by all objects of that class (versus a non-static member variable, of which there is a copy for each object of the class).

Have a look at this part of The Java Tutorial: Understanding Instance and Class Members

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46397
A question which may be related came up on these fora about a week ago, here. See whether it helps you at all.

I agree. Here's the link:
subject: Why constructor is not called. ?
It's not a secret anymore!