Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
65 views

Classes and Objects

1. A class defines a new data type that can be used to create objects, with each object being an instance of that class. 2. A class contains fields (variables) and methods that define its properties and behaviors. 3. To create an object from a class, use the 'new' keyword followed by the class name, which allocates memory and calls the class's constructor.

Uploaded by

Varsha Patil
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views

Classes and Objects

1. A class defines a new data type that can be used to create objects, with each object being an instance of that class. 2. A class contains fields (variables) and methods that define its properties and behaviors. 3. To create an object from a class, use the 'new' keyword followed by the class name, which allocates memory and calls the class's constructor.

Uploaded by

Varsha Patil
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

Classes & Objects

Definition of Class:
Class defines a new data type. Once defined, this new type can be used to create objects of that type. Thus, a class is a template for an object, and an object is an instance of a class. In other words, class is a collection of fields (instance and class variables) and methods.

General Form of Class:


A class is declared by the use of the class keyword. Simplified general form of a class definition is shown below: class classname { type instance-variable1; type instance-variable2; // ... type instance-variableN; type methodname1(parameter-list) { // body of method } type methodname2(parameter-list) { // body of method } // ... type methodnameN(parameter-list) { // body of method } } The general form of a class does not specify a main( ) method. Java classes do not need to have a main( ) method. We only specify one if that class is the starting point for our program. Further, applets dont require a main( ) method at all.

The data, or variables, defined within a class are called instance variables. The code is contained within methods. Collectively, the methods and variables defined within a class are called members of the class. Detail about An instance variable is a variable that is defined in a class, but outside of a method. There is one copy of the variable for every instance (object) created from that class.

A common problem is trying to reference an instance variable from a static method. A static method (eg, main) can only reference static variables in its own class (or its own local variables). Creating object of the class A class is merely a plan for a possible object. It does not by itself create any objects. When a programmer wants to create an object the new operator is used with the name of the class. Creating an object is called instantiation. Object Declaration : An object is declared using a variable name(object name) and type of the objects. Mainclass mc; Object Instantiation : The 'new ' key word is used to create the object. mc = new Mainclass(); Object Initialization (Optional step): The 'new' keyword is followed by a call to a constructor. This call initializes the new object. mc = new Mainclass("Ankit");

You might also like