Showing posts with label Class and Instance step by step in details. Show all posts
Showing posts with label Class and Instance step by step in details. Show all posts

Java Object Class and Instance in Details Step by Step


Object is an instance of a class and entity that has state and behaviour e.g. pencil, book, table, car. State is  value of an object and behaviour is functionality of an object such as writing.

2. Classes:

Class is a template or blueprint that describe the behaviours and states of a particular entity. A class defines new data type and can be used to create object of that type.

A class is declared using class keyword and it can be either abstract, final or default normal class.. The variables defined within a class are called instance variables and instance variables and methods are known as class members.


Class declarations can include following order
o    Modifiers :      A class can be public or default access specifier.

o    Class name:    The name should begin with a initial capital letter.

o    Super class:    Can use extends keyword for inherit.

o    Interfaces:      Can use comma separated implements keyword for inherit.

o    Body :             The class body surrounded by braces, { }.

             Example:     

                    public class Student {

                      String name;

                      String rollNo;

                      int age;

                     String address;

                 }


Here Student is a class and name, rollNo, age, address are properties of Student class.

3. Instance:


An instance is a copy of a Class that representing an Object. When a new instance of a class is created, the JVM will allocate memory for that class instance.



Example of create instance of class.

Student  std = new Student();

Here std is instance of Student class, new keyword creates the physical copy of the object and assign it to the std variable.