Classes & Objects (Part 4)
1. Java Classes
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.
To create a class, use the keyword class
:
A class can contain any of the following variable types.
▪ Local variables − The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed.
▪ Instance variables − Instance variables are variables within a class but outside any method. These variables are initialized when the class is instantiated.
▪ Class variables − Class variables are variables declared within a class, outside any method, with the static keyword.
2. Java Objects
public class MyClass {
public static void main(String args[]) {
MyClass obj = new MyClass(); //create an object named obj
}
}