Core Java

Java Class & Object Tutorial for beginners

Class is a template for creating objects which defines its state and behavior. A class contains field and method to define the state and behavior of its object. 

Syntax for Declaring Class:

Access modifier defines who in java world can access the class and the members of the class.

Unique name for the class in a specific package.
Name of the class which the above class extends.( extends keyword is used for this purpose)
Name of an Interface which the above class implements.( implements keyword is used for this purpose)



Internal structure of Class


Example of  Java Class

Classes are written in a java source file. A source file can contain more then one java class. Below are the rules related to java source code file.


Rules applied to Source code file

  • There can be only one public class per source code file but it can have multiple non public classes.
  • In case there is any public class present in the source code file, name of the file should be the name of the class.
  • Sequence of statements in a source code file should be package >> import  >> Class declaration.
  • No Sequence rule is applied for Comments. Comments can be there in any part of the source code file at any location.
  • Files with no public class can have any name for the class, there is no rule applied for the same.
  • Import and package statements should be applied to all the classes in the same source code file.


How to Create an Object of Class

To create object of  a class <new> Keyword can be used.


<Class_Name>   ClassObjectReference = new <Class_Name>();

Here constructor of the class(Class_Name) will get executed and object will be created(ClassObjectRefrence will hold the reference of created object in memory).


How to Access Member of a Class

(ClassObjectReference.member ). You call a method of an object by naming the object followed by a period (dot), this should be followed by the name of the method and its argument list.

Class Variables – Static Fields

Class variables also known as static fields share characteristics across all objects within a class. When you declare a field to be static, only a single instance of the associated variable is created, which is common to all the objects of that class. Hence when one object changes the value of a class variable, it affects all the objects of the class. We can access a class variable by using the name of the class, and not necessarily using a reference to an individual object within the class. Static variables can be accessed even though no objects of that class exists. It is declared using the static keyword.


Class Methods – Static Methods

Class methods, similar to Class variables can be invoked without having an instance of the class. Class methods are often used to provide global functions for Java programs. For example, methods in the java.lang.Math package are class methods. You cannot call non-static methods from inside a static method.

Java Objects
The Object Class is the super class for all classes in Java. Some of the object class methods are

An object is an instance of a class created using a new operator. The new operator returns a reference to a new instance of a class. This reference can be assigned to a reference variable of the class. The process of creating objects from a class is called instantiation. An object encapsulates state and behavior.

An object reference provides a handle to an object that is created and stored in memory. In Java, objects can only be manipulated via references, which can be stored in variables.

Creating variables of your class type is similar to creating variables of primitive data types, such as integer or float. Each time you create an object, a new set of instance variables comes into existence which defines the characteristics of that object. If you want to create an object of the class and have the reference variable associated with this object, you must also allocate memory for the object by using the new operator. This process is called instantiating an object or creating an object instance.

When you create a new object, you use the new operator to instantiate the object. The new operator returns the location of the object which you assign a reference type.

About the author

V Gautam

Me & Jayshree Singh. Main brain behind this blog/QnA site. We have written all these article as per my personal experience. I have published it mainly to help. These articles might have some problems. So bear with me and let me know if you find any problem in any of the article. You can find me on


  • The objects and class with example shud be mention, beginners don’t know about this and also explain what is n mean by instance of class

    • Their is no link between these two. Variables & method are for two different purpose. Variables are to maintain state while methods are used to do some calculation or perform some business logic.


  • In the introductory para you have said: “In case there is any public class present in the source code file, name of the file should be the name of the class”. Does this mean ( pl see ” In case”) there can be a source code without public class? If so, the file name can be that of any class in the package or any other name also ?

    • Yes. A file can exist without any public class. But if there is a public class file name should match with same.
      In this way you can have more than one public class in a file.


Leave a Comment