Java – Classes and methods

Method is a group of statements to which we give a name so we can execute them. When we call a method, the program first executes the statements in the method and then it continues with the code below the method call.

syntax:

modifier returnType methodName(Parameters){
   statement(s)
}

modifier: (access modifiers) public | private | protected and (non-access modifiers) static | final | abstract

returnType: If the method returns a value then you should specify the type here. Use return as the last statement in the method. If not then void should be used.

methodName: the name of the method

parameters: specify the parameters if any. If no parameters then this is empty.

Class methods and static modifier, <span>Java – Classes and methods</span>, KandZ

Call the methods from the same class.

Class methods and static modifier, <span>Java – Classes and methods</span>, KandZ

Call the methods when you have created an object .

Class methods and static modifier, <span>Java – Classes and methods</span>, KandZ

Method overloading

It is when we declare more than one method with the same name but with different parameters. The number and type of the arguments determines which method will be called

Class methods and static modifier, <span>Java – Classes and methods</span>, KandZ

Static non-access modifier

static has to do with the state and behavior when an object is created. Every object has its instance variables and methods. Static prevents the method/variable to be created for every object. One instance shared by all objects. You can even call a static method without creating the object.

Class methods and static modifier, <span>Java – Classes and methods</span>, KandZ
Class methods and static modifier, <span>Java – Classes and methods</span>, KandZ

THE END!!!

88370cookie-checkJava – Classes and methodsJava – Classes and methods

Related posts

One Thought to “Java – Classes and methods

Comments are closed.