Object Declaration and Access Class Members in JAVA

0
184

Object declaration is necessary to access class members in JAVA. From previous video you have learned how to declare class in JAVA (). This video will teach you the way to declare an object of a class, initialize it with the help of new keyword and finally access class members.

After watching this video you will be able to-

  • Write main function in class.
  • Declare an object of a class.
  • Instantiate object.
  • Access class members using object.
  • Explain the use of dot operator.
  • Explain the use of new keyword.
  • Initialize the property of class in class declaration

Syntax to declare an object for a class:-

<class_name> <object_name>;

new keyword is used to instantiate class object. Syntax to instantiate an object is:

<object_name> = new <class_name>

dot (.) operator is required to access class members using object. Syntax to access a class member is:

<object_name>.<class_member>

class employee
  {
	  int empID=5;
	  void showEmpID()
	  {
		  System.out.println(empID);
	  }
	  public static void main(String args[])
	  {
		  employee e; // object declaration
		  e=new employee();
		  e.showEmpID();
	  }
   }
 

 

How's this tutorial?
Easy0
Difficult0

LEAVE A REPLY

Please enter your comment!
Please enter your name here