Sunday, 25 June 2017

Writing First Class in Java - Class Structure ~ foundjava

Writing a class in java is easy and simple. Here is the structure of the class, it helps to understand the syntax to follow while writing a java class 

modifier class class_name
{
modifier type variable_name;

    modifier return_type method_name(parameters)
    {
    //code;
    }
    public static void main(String args[])
    {
    //code;
    }
}
modifier : Modifier can either be public, private or protected. It depends upon the need.

type : It is the type of the variable. It either represents a primitive datatype like int,float,double,long etc or a class.

return_type : It is the type of value which the method has to return. If method returns nothing then a void should be placed.  

parameters : It represents the parameters that the method need. Parameters are not a mandatory. It depends upon the need. If the method does not need any parameter, the method should be left and void should not be mentioned there 

Example :

public void mymeth(){//code;} // correct

public void mymeth(void){//code;} // wrong

main : It is a must to execute a program. The java interpreter searches for the main method and executes it. Execution is started from the main() method.

No comments:

Post a Comment