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 : 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.
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