Java Class and Object

Posted in Java, Variables and Data Types By Raj On December 13, 2017

 

What is a class?

A class is a blueprint or template or prototype using which you can create any number of individual objects, you can consider that class is a logical entity, and the class contains fields and methods, the definition of class is shown below

if you see the above code the instance variables provides state of the class or object and the method defines the behavior of the object or class

What is an Object?

An object represents a physical entity, the objects are an instance of a class and every instance will have its own behavior and state, the state represents instance variable and behavior represents methods, you can create any number of object’s for the above button code

You can create an object of Button class by using below code, here I am creating three objects which are button1, button2 and button3

The new keyword will create an object in the heap memory, the address of the object will be stored in the stack memory and also the values of the primitive types will be stored in the stack memory

java-object-creation

The class contains instance variables, methods, constructor’s, for more information on variables and constructors refer below-mentioned articles, here I am going to explain methods

What is a method?

The method is a behavior of a class and if you want to execute set of statements to solver particular business problem then you can use methods, you can have more than one method within a class ,generally the methods which are in the class are always unique but, if you want to have the same name for more than one method then you must use method overloading,

Method declaration

Examples :

Output: