Is it necessary to have a constructor in every class in Java

class Temp
{ 
   {
      System.out.println("this is in it block");
   }


   public static void main(String... s)
   {
      System.out.println("abhi");
   }
}

The above code does not print the content of the init block but we know that init block is first line of the constructor.

But on the other hand if I use new Temp(); it get printed. Why so?

If constructor is in every class the above code should also print it without object intialisation. Shouldn't it?

Answers


There is nothing in your code that calls a constructor, so the instance init block never gets called either.

If constructor is in every class the above code should also print it without object intialisation. Shouldn't it?

No. Why should it? You would have to call it for that to happen. It won't run just because it is present.


You don't need to code a constructor in a class. The system will generate a default constructor for you automatically if you don't have any constructor. Your inner block is called an instance initializer; it is run after when a constructor executes, after any calls to super(...) in the constructor and before the (rest of) the constructor body. (If the constructor starts with a call to this(...), the instance initializer is not run for that constructor.) However, you do need to create an instance of the class for the constructor and any instance initializer to be executed. (If you made the inner block static it would be a static initializer and would run exactly once, when the class object was initialized, regardless of whether instances were created.)


{ System.out.println("this is in it block");

}

Get called for each object you create. static {} get called even if you don't create object. See static-and-instance-initializers-in-java or static-and-instance-initializers-in-java for more details.


Hi if you run this code You will understand the difference between instances block and static block.

The static block it is the first to run and only run once, the first time a static method is called either the first time an instance is initiate. Like in your code you are calling a static method (main) you should create a static block, but it is call only once.

The instance block, it runs always that it is initiate an instance.

class Temp
{
    {System.out.println("this is an instance block");}

    static {System.out.println("this is a static block");}

    public static void main(String... s){
        System.out.println("This a static method");
        Temp myTemp = new myTemp();
        myTemp.myMethod();
    }

    void myMethod (){
        System.out.println("This an instance method");
    }
}

In the other hand if you do not type any construct Java will create one for you

Temp(){//Or whatever name of your class
    super();
}

Need Your Help

Getting the IP Address of an Android Device using it's hotspot from another android device

android android-wifi

I have two android enabled devices.On one device i turned on the hotspot and from other device i am connecting to that hotspot. Now i want to get the IP address of the first device. How can i get it.

Animate div from middle

jquery jquery-animate

I have this code. I want the div to animate from middle and cover whole space .like this

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.