Structure of a Java class (OCAJP 1.2)

Class is a template or a blueprint which is used to describe objects.
Class is an most important entity of any Java Program.
You can't write a Java program without using class or interface or enum.

A java class is written inside a source code file.
A single source code file can hold more than one java class.

Source File

Source File is a the file with extension .java which contains the java class.

Java Class

Class is the place where we write everything we want. Class is a collection of methods and constructors.


Methods is a block which performs certain task. Anything required to be performed goes inside the curly braces of method.

How does a class look like?

Anatomy of a class.

The image above clearly indicates how to write a class.

There are some rules to be followed while writing a class.

Source File Declaration Rule.

  • A single .java file can only have one public class. No two classes can be public.
  • The class with main method should be public.
  • Name of the class with main method and name of the .java file should be same. Let say you have 3 classes in a .java file Pen,Dog, and Car but main() is in Car, so the name of .java file should be Car.java or if none of them have main() in that case class which is of nature public should be the name of .java file.
  • If class is being packaged, package statement should be the first statement in the class.
  • If class is using some imports, import statement should be used before declaring the class, in case package and import both are used they should be used in order like package then import then class.

public static void main(String[] args)

Almost everyone using Java is familiar with main().
main() is the part where Java Program starts executing. 

Only naming method as main doesn't mean that it has got super powers, JVm give it super power only when it comes in the given syntax.

public static void main(String[] args)

- Now method main is public so that it can accessed from anywhere.
- It returns nothing hence void.
- It take a String[] of name args, args is just a name of object so it can be any valid identifier.
- main is of nature static. Why?

Why is method main() static in nature?

  • All the static variables and methods get loaded when the class is loaded.
  • When a class have a method main() JVM calls it directly without using the object of that class, so to make the class available for JVM, we need to write the method main() of nature static.
  • If main() is not static, JVM would require the instance of the class to call it, but we instantiate the object itself inside main method.
Let say for the following class
class MainDemo 
 public static void main(String[] args) 
  MainDemo md=new MainDemo();

 public void m1()
  System.out.println("m1()- Invoked");

When we execute this program, JVM takes the control and calls the main method 

JVM must be doing this,


Hence initializing the start of program.

This post was more of technical details about a class. If you need to understand the concept of class, why it is used and why Object Oriented programming language use class Click Here.

1 comment: Leave Your Comments