Java Class & Object Tutorial for beginners

This entry is part 3 of 33 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 initiated.
  • 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 >>

14 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.

  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.



Leave A Comment

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