





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






Content:
● class is a keyword used to define class ● class name is the identifier that specifies the name of the class ● type specifies the datatype of the variable ● instance_variable1,... are the variable defined in the class ● method name is the method defined in the class that can operate on the variables in the class NOTE: The variables defined in the class are called member variables/data members The functions defined in the class are called member functions/member methods. Both data members and member methods together called members of class.
Example: class sample { int x, y; setXY() { x=10; y=20; } }
Accessing members of an object: Once an object of a class is created we can access the members of the class through the use of object name and ‘.‘ (dot) operator. Syntax: objectname. member; Example: class sample { int x, y; setXY() { x=10; y=20; } printXY() { System.out.print(“=”+x); System.out.print(“=”+y); } class Demo { public static void main(String ar[]) { Sample s; s=new String(); s.setXY(); s.printXY(); } }
Constructor: A constructor is a special kind of method that has the same name as that of class in which it is defined. It is used to automaticallyl initialize an object when the object is created. Constructor gets executed automatically at the time of creating the object. A constructor doesn’t have any return type. Example: // program to demonstrate constructor class Student { int number; String name; Student() // default constructor { number=569; name=”satyam”; } Student(int no, String s) // parameterized constructor { number=no; name=s; } void showStudent() { System.out.println(“number =”+number); System.out.println(“name =”+name); } }