Skip to content

Class in Java

EWriter edited this page Dec 23, 2021 · 3 revisions

A class is a group of objects which have common properties. It is a template or blueprint from which objects are created. It is a logical entity. It can't be physical.

A class in Java can contain:

Fields
Methods
Constructors
Blocks
Nested class and interface

Syntax to declare a class:

class <class_name>{  
    field;  
    method;  
}    

Instance variable - A variable which is created inside the class but outside the method is known as an instance variable. Instance variable doesn't get memory at compile time. It gets memory at runtime when an object or instance is created. That is why it is known as an instance variable.

method - a function which is used to expose the behavior of an object.

'new' keyword - used to allocate memory at runtime.

Clone this wiki locally