Classes

Classes:
In most of the programs, we had used main() method, which is used to demonstrate the basic syntax of java. In java, all code is grouped into classes. A class is thus a code container. the definition of the class starts with an access modifier (public in this case), which specifies which classes have access to it.this is followed by keywords of class and the name of the class.

classes in java
java classes

For example:

class Empty
{
---
---
--
}

The above body of the class is empty therefore it does not contain any function or methods so it can’t do anything.


THE GENERAL CLASS:
A class is declared by the use of the class keyword. the classes that have been used up to this point are actually very limited.

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
}
}
The data,or variables,defined within a class are called instance variables.

Declaring object:
you can do this with a new operator.the new operator. The new operator dynamically allocates memory for an object and returns a reference to it.
for example of object as a box:


Box mybox=new Box();
Translate »