A class is the blueprint from which individual objects are created.
In the real world, you'll often find many individual objects all of the same kind. There may be thousands of other bicycles in existence, all of the same make and model. Each bicycle was built from the same set of blueprints and therefore contains the same components.
In object-oriented terms, we say that your bicycle is an instance of the class of objects known as bicycles.
Declaration Of Class
class ClassName { //Declare Variable //Declare Functions } OR class Student { int rollno; String name; public void getDetails() { } public void showDetails() { } }
A class can contain any of the following variable types.
Local variables Variables defined inside methods, constructors or blocks are called 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 instantiated when the class is loaded. Instance variables can be accessed from inside any method, constructor or blocks of that particular class.
Class variables Class variables are variables declared with in a class, outside any method, with the static keyword.
Object is an instance of a class. Class is a template or blueprint from which objects are created. So object is the instance(result) of a class.
Objects are key to understanding object-oriented technology. Look around right now and you'll find many examples of real-world objects: your dog, your desk, student, your television set, your bicycle.
An object has three characteristics:
state: represents data (value or variables) of an object.
behavior: represents the behavior (functionality or methods) of an object such as getDetails,showDetails,addition,subtraction etc.
identity: Object identity is typically implemented via a unique ID. The value of the ID is not visible to the external user. But,it is used internally by the JVM to identify each object uniquely.
Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail).
Bicycles also have state (current gear, current pedal cadence, current speed) and behavior (changing gear, changing pedal cadence, applying brakes). Identifying the state and behavior for real-world objects is a great way to begin thinking in terms of object-oriented programming.
Take a minute right now to observe the real-world objects that are in your immediate area. For each object that you see, ask yourself two questions:
"What possible states can this object be in?" and "What possible behavior can this object perform?".
As you do, you'll notice that real-world objects vary in complexity. Your desktop lamp may have only two possible states (on and off) and two possible behaviors (turn on, turn off), but your desktop radio might have additional states (on, off, current volume, current station) and behavior (turn on, turn off, increase volume, decrease volume, seek, scan, and tune).
You may also notice that some objects, in turn, will also contain other objects. These real-world observations all translate into the world of object-oriented programming.
In Java, the new key word is used to create new objects.
There are three steps when creating an object from a class:
Declaration: A variable declaration with a variable name with an object type.
Instantiation: The new keyword is used to create the object.
Initialization: The new keyword is followed by a call to a constructor. This call initializes the new object.
public class Student{ public Student(String name){ // This constructor has one parameter, name. System.out.println("Name is :" + name ); } public static void main(String []args){ // Following statement would create an object ob Student ob = new Student("Ravi"); } }
Instance variables and methods are accessed via created objects. To access an instance variable the fully qualified path should be as follows
/* First create an object */
Student ob = new Student( "Ravi");
/* Now call a variable as follows */
ob.variablename;
/* Now you can call a class method as follows */
ob.MethodName();
public class Student { String name; public Student(String name){ // This constructor has one parameter, name. this.name=name } public static void main(String[] args){ /* Object creation */ Student ob = new Student("Ravi"); /* You can access instance variable as follows as well */ System.out.println("Name Is:" + ob.name); } }