Why do we need a class in Java?
Java is an object-oriented programming language and represents data in the form of Objects. So in order to create an object, we required a class.
What is a class?
A class is a user-defined data type in Java & is a Structure representing data and logic.
A class is a specification or blueprint or template of an object. A class defines the structure, state, and behavior of mean data & code that can be shared by a set of objects.
The data defined by the class are referred to as member variables or instance variables or attributes.
Syntax to declare a class:
Class<class name>
{
Type instance variable 1;
Type instance variable 2;
…..……………………………….
Type instance variable N;
Type methodname1(parameter list)
{
//body of method//
}
Type methodname2(parameter list)
{
//body of method//
}
……………………………………
Type methodnameN(parameter list)
{
//body of method//
}
}
|
Declaring a variable: In Java, all variables must be declared before they can be used.
Variable declaration is shown below:
Type identifier [ = value], identifier [ = value], ………;
int a, b, c; // declares three integers a, b, c;
int i = 1, j = 2, k; //declares three more integers & initializing i & j;
Class A
{ int i,j;
Void funA()
{ System.out.println(“FunA() of class A”);
} }
|
Understanding the above program:
1) Where “A” is the name of the class.
2) “i& j” are instance variables.
3) “funA()” is the name of the method (or) function.
Why do we need an Object?
In order to load the members of the class(means instance variables, methods) from the byte code to the Ram in the form of executable format and make them available to the program which is under execution. Without using the concept of an object, we can’t load the members of the class from the byte code to the RAM at runtime.
An object is an instance of a class. The memory space which is allocated for the members of a class, dynamically at the runtime can be called an object.
What is the Instance?
Any dynamic memory allocation can be called an Instance. That is memory space allocated for anything during runtime can be called an Instance.
Syntax to declare Object:
<Class Name> ClassObjectReference = new <Class Name>();
Object Example for Above program:
Ways to create Object: There are various ways to create an Object. The most commonly used and the simple and easiest way to create Object is using New keyword;
class Objectexample
{
public static void main(String[] args)
{
Objectexample e = new Objectexample();
System.out.println("Example of Object using th new keyword");
System.out.println("E="+e);
}
}
|
Example of Object using the new keyword
E = Objectexample@42e922
|
Types of Objects In Java:
In Java, we have two types of objects based on the referenced variables. They are:
1) Referenced (or) Reachable Object.
2) In-referenced (or) Un-reachable Object.
Referenced (or) Reachable Object: If an object is pointed by at least one referenced variable, it is called Referenced (or) Reachable object.
Example of Referenced Objetc creation :
ObjectExample e = new ObjectExample();
Program of Referenced (or) Reachable Object:
class Objectexample
{
public static void main(String[] args)
{
Objectexample e = new Objectexample();
System.out.println("Example of Referenced Object");
System.out.println("E="+e);
}
}
|
Example of Referenced Object
E = Objectexample@52e922
|
Un-Referenced (or) Un-Reachable Object: If an object is not pointed by at least one referenced variable, it is called Un-Referenced (or) Un-Reachable object.
Example of Un-Referenced Object creation: new Object Example();
Program of Un-Referenced (or) Un-Reachable Object:
class Objectexample1
{
public static void main(String[] args)
{
new Objectexample1();
System.out.println("Example of Un-Referenced Object");
System.out.println("E="+ new Objectexample1());
}
}
|
Example of Un-Referenced Object
E = Objectexample1@52e922
|
The Five different locations where we can define Objects in Java are:
1) Defining Object has local to a function.
2) Defining Object has an instance variable (or) Non-static member of a class.
3) Defining Object has the parameter of the function.
4) Defining Object has a return data type of the function.
5) Defining Object has a Static variable of the class.
Defining Object has local to a function:
class Demo1
{
int x,y;
void fun1()
{
x = x + 1;
A a1 = new A();
a1.i = x + 1;
}
public static void main(String[] args)
{
A a1 = new A();
Demo1 d1 = new Demo1();
d1.x = 2;
System.out.println("a1.i = "+ a1.i);
System.out.println("d1.x = "+ d1.x);
}
}
|
Defining Object has an instance variable (or) Non-static member of a class.
class Demo2
{
int x = 1;
A a1 = new A();
public static void main(String[] args)
{
Demo2 d1 = new Demo2();
System.out.println(d1.a1);
System.out.println(d1.a1.i);
d1.a1.funA();
}
}
|
Defining Object has a parameter of the function.
class Demo3
{
void fun1(int x)
{
int sq = x*x;
System.out.println("sq = " +sq);
}
public static void main(String[] args)
{
Demo3 d1 = new Demo3();
d1.fun1(5);
int x = 7;
d1.fun1(x);
}
}
|
Defining Object has return data type of the function:
class Demo4
{
int getSq(int x)
{
int sq = x * x;
return(sq);
}
public static void main(String[] args)
{
Demo4 d1 = new Demo4();
int x = d1.getSq(2);
System.out.println("X = " +x);
System.out.println(d1.getSq(4));
int y = d1.getSq(5) + 2;
System.out.println("Y = " +y);
}
}
|
Defining Object has a Static variable of the class.
class Demo5
{
static int x = 1;
static A a1 = new A();
}
|
class Demo6
{
public static void main(String[] args)
{
System.out.println(Demo5.x);
System.out.println(Demo5.a1);
System.out.println(Demo5.a1.i);
Demo5.a1.funA();
}
}
|
Also Read: C String Program with output Java Constructor