Classes and Objects:
How to create a class in java?
Java is an object-oriented programming language.
Everything in Java is associated with classes and objects, along with its attributes and methods. For
example: in real life, a car is an object. The car has attributes, such as weight and color,
and methods, such as drive and brake.
A Class is like an object constructor, or a "blueprint" for creating objects.
Create a Class
To create a class, use the keyword class:
[Link]
Create a class named "Main" with a variable x:
Class Declaration in Java:
access_modifier class<class_name> {
data member(variable);
method;
constructor;
nested class;
interface
Syntax: public class Main {
int x = 5;
Properties of Java Classes:
Class is not a real-world entity. It is just a template or blueprint, or a prototype from which
objects are created.
A class itself does not occupy memory for its attributes(variables) and methods until an
object is instantiated.
A Class in Java can contain Data members, methods, a Constructor, Nested classes, and
interfaces.
Java Objects:
How to declare objects in java?
Objects are the instances of a class that are created to use the attributes and
methods of a class. A typical Java program creates many objects, which as you know, interact by
invoking methods. An object consists of:
State: It is represented by attributes of an object. It also reflects the properties of an object.
Behavior: It is represented by the methods of an object. It also reflects the response of an
object with other objects.
Identity: It gives a unique name to an object and enables one object to interact with other
objects.