- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are the differences between a static block and a constructor in Java?
Static block
- The static blocks are executed at the time of class loading.
- The static blocks are executed before running the main () method.
- The static blocks don't have any name in its prototype.
- If we want any logic that needs to be executed at the time of class loading that logic needs to placed inside the static block so that it will be executed at the time of class loading.
Syntax
static { //some statements }
Example
public class StaticBlockTest { static { System.out.println("Static Block!"); } public static void main(String args[]) { System.out.println("Welcome to Tutorials Point!"); } }
Output
Static Block! Welcome to Tutorials Point!
Constructor
- A Constructor will be executed while creating an object in Java.
- A Constructor is called while creating an object of a class.
- The name of a constructor must be always the same name as a class.
- A Constructor is called only once for an object and it is called as many times as we can create an object. i.e The constructor gets executed automatically when the object is created.
Syntax
public class MyClass { //This is the constructor MyClass() { // some statements } }
Example
public class ConstructorTest { static { //static block System.out.println("In Static Block!"); } public ConstructorTest() { System.out.println("In a first constructor!"); } public ConstructorTest(int c) { System.out.println("In a second constructor!"); } public static void main(String args[]) { ConstructorTest ct1 = new ConstructorTest(); ConstructorTest ct2 = new ConstructorTest(10); } }
Output
In Static Block! In a first constructor! In a second constructor!
- Related Articles
- What are the differences between default constructor and parameterized constructor in Java?
- What are the differences between a static and a non-static class in C#?
- What are the differences between import and static import statements in Java?
- What are differences between static binding and dynamic binding in Java?
- Sequence of execution of, instance method, static block and constructor in java?
- What are the restrictions imposed on a static method or a static block of code in java?
- A static initialization block in Java
- What are the differences between static, rolling and sliding friction?
- Difference between Static Constructor and Instance Constructor in C#
- A non-static initialization block in Java
- Differences between Method Reference and Constructor Reference in Java?
- What are the differences between a MouseListener and a MouseMotionListener in Java?
- What are the differences between a JComboBox and a JList in Java?
- What are the differences between a JScrollBar and a JScrollPane in Java?
- What are the differences between a JTextPane and a JEditorPane in Java?

Advertisements