Class Declarations in Java Programming

Class Declarations in Java Programming

Previous Home Next

 

A class is a source code blue print for the object.that blueprint specifies each objects behavior and state variables. 
The data an object holds and how it manipulates that data is specified through the creation of a class.

    In general, class declarations can include these components, in order:

    Modifiers such as public, private, and a number of others that you will encounter later.
    The class name, with the initial letter capitalized by convention.
    The name of the superclass, if any, preceded by the keyword extends. A class can only extend one subclass.
    A comma-separated list of interfaces implemented by the class, if any, preceded by the keyword implements.
    A class can implement more than one interface.
    The class body, surrounded by braces, {}.

 Declaration of class
Access modifiers( public)( abstract | final) class class_name
{
     // behaviors and state variables are declared between the { and } characters
}


 

class Demo
{
int i = 6;
public static void main (String [] args)
{
Demo obj1 = new Demo ();
System.out.println ("obj1.i = " + obj1.i);
obj1.printHello ();
Demo obj2 = new Demo ();
obj1.i = 5;
System.out.println ("obj1.i = " + obj1.i);
obj1.printHello ();
System.out.println ("obj2.i = " + obj2.i);
obj2.printHello ();
}
void printHello ()
{
System.out.println ("Hello! i = " + i + "\n");
}
}


Previous Home Next