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();
Top comments (0)