This content originally appeared on DEV Community and was authored by vishnu vishnu
What is class?
A class in Java is a Blueprint or template from which an object is created. The object is defined by the Structure and behaviour(methods) that objects of that class will possess.
It is a logical entity.
No memory is allocated when a class is defined.
All classes are non-primitive datatypes.
How to create a class?
**
BASIC SYNTAX for creating a class:**
public class Car
{
// Data members (fields)- hold the actual value
String brand;
int speed;
//Behaviour (method)
void drive()
{
System.out.println("driving a car");
}
}
//But it’s not a real car yet. It’s just the plan.
Class Name Rules:
- Must start with an Uppercase first letter or (_) underscore or dollar sign ($).
- can not start with a number
- cannot contain spaces or special characters
- Use Pascal-case for naming
What is an Object?
An object is an instance of a class, and it has its own memory copy. It is a physical entity. An object is created with the new keyword.
When a class is created, no memory is used. Memory is allocated only when an object is created from that class.
Creating an Object :
classname, reference var,assignment operator,new keyword,classname();
Student id = new Student();
This content originally appeared on DEV Community and was authored by vishnu vishnu
vishnu vishnu | Sciencx (2025-11-26T01:23:12+00:00) Class and Object in java. Retrieved from https://www.scien.cx/2025/11/26/class-and-object-in-java-2/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.