Easy Tutorial
For Competitive Exams

Java Programming Classes and Objects

What is Java class?

A class can be defined as a template or blueprint which describes the states and behaviours of an object.

How to declare a class?

A class is declared by the use of the class keyword.

Syntax

class classname
{
	
     datatype instance-variables;
     datatype methodname(parameter-list)
	{
		// body of the method
	}
	
}

What does Java Class Consist?

Fields or Variables

Field is nothing but the property of the class or object which we are going to create .

Example: if we are creating a class called computer then they have property like model, mem_size, hd_size, os_type etc.,

Methods

Method is nothing but the operation that an object can perform it define the behavior of object how an object can interact with outside world .startMethod (), shutdownMethod ().

Print the value of variable

public class Demo {
    public static void main(String [] args) {
          int a=50;
        System.out.println(a);
    }
}
Try it Yourself

What is Objects in Java?

Object is an instance of the class.It is a bundle of related variables and functions (also known methods).

Objects share three characteristics: They have Identity,State and Behavior.

State: State is a well defined condition of an item. A state captures the relevant aspects of an object.

Behavior: Behavior is the observable effects of an operation or event.

Identity:name of the Object.

Example

Object: House(identity)
State: Current Location, Color, Area of House, etc.,
Behavior: Close/Open main door.

Solve this!!

11241.Attribute of an object can include information about
State
Behaviour
Function
Procedure

How to declare a object?

To declare an object you must

  • Give the object a name
  • Specify what type (i.e. class) the object will be.

Syntax

<class name><object name>;
Example: Player player; 

How to create a Object?

To create an object,use the syntax

Syntax

<object name> = new <class name> ();
Example: creating a Player object
player = new Player(); 

Declaration and creation can be done all in one step.

Example

Player player = new Player(); 

Example

class Student{  
 int id;  
 String name;  
}  

class Test{  
 public static void main(String args[]){  
  Student s1=new Student(); 
 
  s1.id=100;  
  s1.name="aaa";  
  System.out.println(s1.id+" "+s1.name);
 }  
}  
Try it Yourself

Solve this!!

11238.Which of the following is a valid initialization of an object of class Box?
Box obj = new Box();
Box obj = new Box;
obj = new Box();
new Box obj;
11239.What is the output of this program?
class box {
        int width;
        int height;
        int length;
} 
class mainclass {
        public static void main(String args[]) {        
             box obj = new box();
             obj.width = 10;
             obj.height = 2;
             obj.length = 10;
             int y = obj.width * obj.height * obj.length; 
             System.out.print(y);
      } 
}
100
200
300
400
11240.What is the output of this program?
class box {
        int width;
        int height;
        int length;
} 
class mainclass {
    public static void main(String args[]){        
            box obj = new box();
            System.out.println(obj);
      } 
}
0
No value
Garbage value
Error
Explanation:
Object obj of box class contains reference to the memory which was given to its class instances. Printing obj will print the address of the memory.
Share with Friends