Java Class & Object Tutorial for beginners

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


Syntax for Declaring Class:

Access modifier define who in java world can access this Class and member of the class.

Unique name for the class in specific package.
Name of class which above class extends.(Super class of the given class)
Name of Inerface ‘Class_Name‘ class implements.(Implements keyword is used for this purpose)



Internal structure of Class


Example of  Java Class

Classes are written in 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 could be only one public class  per source code file but it can have multiple non public class.
  • In case there is any public class present in source code file, name of the file should be the class name.
  • Sequence of difference statement in source code file would 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.
  • File with no public class can have any name there is no rule applied for the same.
  • Import and package statements applied to all the classes in 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 get created(ClassObjectRefrence will hold the refrence 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), followed by the name of the method and its argument list, like this:

Class Variables – Static Fields

Class variables also know 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 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 exist. It is declared using 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 o a reference type.

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


  1. Java Dev   •  

    It will be great if you will underline the difference between the class and object. It’s very important for the beginner java developers.

  2. bhawna   •  

    awesome…. its really good for beginners….

  3. Pingback: Java Beginners TutorialGetting started with Java

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">