Java Class & Object Tutorial for beginners

This entry is part 3 of 34 in the series Core Java Course

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 :  It defines who in java world can access this class and members of the class.

Class_Name : Unique name for the class in a specific package.
Super_Class_Name : Name of the class which given class extends.( extends keyword is used for this purpose)
Interface_Name : Name of an Interface which 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. There are some rules associated to Java source file. Below you will find the list of Rules.

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.

What is Object

We always get confused with Class and Objects. And people always ask question what is difference between Class and Object. Class is like you are creating a prototype of something and Object is actual implementation of that prototype. In technical word Class is a template which describe what are all state or behavior an instance of this class can have. And Object is actual implementation of that class which have some state & behavior in form of variable and methods and it acquire some memory space.

How to Create an Object of a 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).


Object Creation


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.

Bullet Points

  • Class can have only public and default access.
  • Public class needs to be in same name java file.
  • Single java file can contain more then one non public class but can have only one public class.
  • A public class can be seen by all classes from all package
  • A class with default access can be seen only by classes within the same package.
  • Java file with no public class have no naming restriction.
  • Class can also have final and abstract & strictfp non access modifiers.
  • An abstract class can not be instantiated.
  • A final class can not be subclassed.
  • A class can not be both final and abstract.
  • Class visibility can be seen in 3 parameter
    1. If a class can extend another class?
    2. If a class can create instance of another class?
    3. If a class can access methods and variable of another class?
Series Navigation<< jdk vs jre vs jvmConstructors in Java >>

23 Comments Java Class & Object Tutorial for beginners

  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.

    1. Pushp Singh

      I am giving you a very basic and easy definition between class and object in java:

      1. “Class” in java can be assumed as any Infrastructure or building, and we create room
      withing that building to store our valuable, so this room can be assumed as “object.”
      2. Just like without building we cannot create rooms, like wise only without class we can
      not create object.
      3. Rooms are always inside the building, like wise objects are always inside the class.

      **** Now, coming towards accessing the objects:
      # There are 4 Access Specifier of any class:
      1. public 2. private 3. protected 4. default

      1. Any one can access to any rooms of the public building, like that only we can access
      objects of public class.
      2. Only the Residents can access to the room of the private building like wise only objects of
      private class are accessed inside the class only.
      3. If two friends are staying in one room of the same building and then after sometime one
      got shifted to another room of the same building, then there is a case that the guy who
      shifted to other room can have access to the another guy room, like wise only the object
      of the protected class can be accessed withing the same class as well as by all objects of
      that class.
      4. default class are known as “Package-private class” it is visible withing the same package

  2. Pingback: Java Beginners TutorialGetting started with Java

  3. shruti

    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

    1. Vivekanand Gautam

      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.


  4. Balan

    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 ?

    1. Vivekanand Gautam

      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.


  5. Madhavi

    In Bullet Points….. “An abstract class cannot be initiated” it should be “An abstract cannot be instatiated”…. please verify it


Leave A Comment

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