Method is a sub block of a class that contains logic of that class.
logic must be placed inside a method, not directly at class level,
if we place logic at class level compiler throws an error.
So class level we are allowed to place variables and methods.
The logical statements such as method calls, calculations and printing related statements must be
placed inside method,
because these statements are considered as logic.
Syntax of method:
accessmodifier keyword(s) returntype methodname(){
write the logic //method body
}
1.Method Prototype:
The head portion of the method is called method prototype.
Ex:public static void main(String args[]) // ->method prototype.
2.Method body and logic:
The "{ }" region is called method body, and the statements placed inside method body is called logic.
3.Method parameters and arguments:
The variables declared in method parenthesis "( )" are called parameters.
We can define method with 0 to n number of parameters.
The values passing to those parameters are called arguments.
In method invocation we must pass arguments according to the parameters order and type.
syntax of parameterised method:
accessmodifier keyword(s) returntype methodname(datatype p1,datatype p2,datatype p3..){logic}
ex:
public static void add(int a, float b){ // ->method prototype. int a, int b are parameters
float sum=a+b;
}
//calling the add() method.
public static void main(String[] args){
add(5,10.5F); //calling/invocation of the parameterised method
add(15,20.2F);
4:Method signature:
The combination of method "name + parameters " is called method signature
In the above program add(int a, float b) and main(String args[]) are method signatures.
5. Method return type:
The keyword that is placed before method name is called method return type.
It tells to compiler and JVM about the type of the value is returned from this method after its execution
If nothing is return by method then we can use "void" keyword which specifies method returns nothing.
return type keywords:
void,boolean,char,byte,short,int,long,float,double,String,interface,classname,arrayname[],collectiontypes, map.
Methods are two types:
1)static method
2)nonstatic method
Static Methods in Java
It is common to often refer to static methods in Java as class methods. The reason is that the static members are associated with the classes and their objects. Similar to static variables, static methods can also be invoked using the class name. There are some important points that you need to consider when you work with static methods in Java. These are -
- The static methods of a particular class can only access the static variables and can change them.
- A static method can only call other static methods.
- Static methods can’t refer to non-static variables or methods.
- Static methods can’t refer to “super” or “this” members.
Also, often you will notice that the main method in Java is defined as static. This is so because you don’t need an object to call the main method in Java. If you have defined the main method in Java as non-static, then the Java Virtual Machine (JVM) would have first created an instance for the main class and then called the main method using that instance which would lead to unnecessary memory usage. Moreover, there are tons of static methods defined in the Wrapper Classes and Utility Classes in Java.
Any static method can call any other static method in the same file or any static method in a Java library like Math.static methods can be called inside nonstatic/instance methods also.
Syntax:
accessmodifier static void methodName(){
logic
}
The static method has two types:
1) non-parameterized static method: if a method does not contain any parameters inside the parenthesis ()then that method is called a non parameterized static method
access modifier static void methodName(){
logic
}
parameterized static method: if a method contains parameters inside the parenthesis() then that method is called a parameterized static method
syntax:
access modifier static void methodName(datatype p1,datatype p2,...){
logic
}
Note: if you don't write a void keyword in the method signature line then static method syntax?
access modifier static datatype methodname(){
logic;
return value;
}
accessmodifier static datatype methodname(datatype p1, datatype p2,.....){
logic;
return value;
}
Next, consider the below example.
class Test{
int counter;
public static void increment(){
counter++;
System.out.println("Current value of Counter is: " + counter);
}
}
class Main{
public static void main(String args[]){
Test.increment();
}
}
The above program will generate an error. This is so because it has tried to access a non-static variable called counter inside a static method called increment().
Let’s try to use the same example but this time, you will specify the counter variable as a static variable.
class Test{
static int counter;
public static void increment(){
counter++;
System.out.println("Current value of Counter is: " + counter);
}
}
class Main{
public static void main(String args[]){
Test.increment();
Test.increment();
Test.increment();
}
}
Ans)staticmethodname();
Q)how can you call static void parameterised method under main() in same class?
Ans)staticmethodname(p1value,p2value,p3value..);
Q)how can you call return type static method() under main() in the same class?
datatypeofthemethod varaiblename=returntypestaticmethod();
System.out.println("value is"+varaiblename);
(or)
how to call returntype static method in printstatement?
Ans)System.out.println("value is"+returntypestaticmethod());
Q)how can you access static variable in same class under any static/nostatic/main method?
Ans)directly call static variable
================================================
Scenrio:2 how to call one class static members in another class of same package
Q)How can you access one class static variable in another class?
Ans)classname.staticvariable;
Q)how can you call one class static void method in another class?
Ans)classname.staticmethod();
Q)how can you call static void parameterised method under any method/main() in another class?
Ans)classname.staticmethodname(p1value,p2value,p3value..);
Q)how can you call return type static method() under any method/main() in another class?
datatypeofthemethod varaiblename=classname.returntypestaticmethod();
System.out.println("value is"+varaiblename);
(or)
how to call returntype static method in printstatement?
Ans)System.out.println("value is"+classname.returntypestaticmethod());
Scenrio:3 how to call one class static members in another package class
import packagename.classname;
Q)How can you access one class static variable in another package class?
Ans)classname.staticvariable;
Q)how can you call one class static void method in another package class?
Ans)classname.staticmethod();
Q)how can you call static void parameterised method under main() in another package class?
Ans)classname.staticmethodname(p1value,p2value,p3value..);
Q)how can you call return type static method() under main() in another package class?
datatypeofthemethod varaiblename=classname.returntypestaticmethod();
System.out.println("value is"+varaiblename);
(or)
how to call returntype static method in printstatement?
Ans)System.out.println("value is"+classname.returntypestaticmethod());
=================================================
(or)
STATIC IMPORT:
Static imports allow you to call static members, i.e., methods and fields of a class directly
without specifying the class.
Using static imports greatly improves the readability of your test code, you should use it.
how to import static members in another class?
import static packagename.classname.staticmember;
import static packagename.classname.*;
no need to refer class name before static members.
staticmethodname();
staticvariable;
//calling returntypestaticmethod
datatype variablename=returntypestaticmethod();
===============================================
nonstatic/instance method():if a method doesnot contain static keyword then that method is called non static method.
Syntax:
accessmodifier returntype methodName(){
method body(logic)
}
parameterised instance method():if a method contains parameters then that method is called parameterised instance method.
Syntax:
accessmodifier void methodName(datatype p1, datatype p2, datatype p3,..){
method body(logic)
}
ex:
public void setEmpName(String name){method body}
non parameterised instance method:if a method doesnot contain any parameters then that method is called non parameterised instance method.
syntax:
accessmodifier void methodName(){
method body(logic)
}
Without void keyword instance method syntax:
accessmodifier datatype methodName(){
logic;
return variable;
}
ex:
protected String getEmpName(){
return name;
}
accessmodifier datatype methodName(datatype p1,datatype p2){
logic;
return variable;
}
How can you access/execute non static members under main method/any other static method?
Ans)by creating object for that class.
How to create object for a class?
Ans)by using new keyword
//local variable syntax:
classname refvariable=new classname();
reference variable:if a variable points to the object is called referencevaiable.
referencevaiable holds the address of the object
//calling the non static method
refvariable.nonstatic/instancedmethod();
//calling non static variable
refvariable.nonstatic variable;
Anonymous Object:create an object for a class without reference variable then that object is called anonymous object
Syntax:
new classname(); //anonymous object
//caling the non static method with anonymous object
new classname().nonstatic method1();
new classname().nonstatic method2();
In object-oriented programming, a class is a template for creating objects. You can create multiple instances of a class, each of which is called an object. Each object can have its own attribute values (data) and behavior (methods).
========================================================================
Scenario 1)In the Same class how to call nonstatic members
create object for the class
classname objreference=new classname();
Q)How can you call nonstatic void method under main method in the same class?
Ans)objreference.nonstaticmethod();
Q)how can you call nonstatic void parameterised method under main() in same class?
Ans)objreference.nonstaticmethodname(p1value,p2value,p3value..);
Q)how can you call return type nonstatic method() under main() in the same class?
datatypeofthemethod varaiblename=objreference.returntypenonstaticmethod();
System.out.println("value is"+varaiblename);
(or)
how to call returntype nonstatic method in printstatement?
Ans)System.out.println("value is"+objreference.returntypenonstaticmethod());
Q)how can you access nonstatic variable in same class under main method?
Ans)datatype output=objectreference.nonstatic variable1+objectreference.nonstatic variable2;
System.out.println("nonstatic variable value"+objectreference.nonstatic variable);
Scenrio:2 how to call nonstatic members in another class of same package
create object for the class
classname objreference
Q)How can you access one class nonstatic variable in another class?
Ans)objectreference.nonstatic variable;
Q)how can you call one class nonstatic void method in another class?
Ans)objectreference.nonstaticmethod();
Q)how can you call nonstatic void parameterised method under main() in another class?
Ans)objectreference.nonstaticmethodname(p1value,p2value,p3value..);
Q)how can you call return type nonstatic method() under main() in another class?
datatypeofthemethod varaiblename=objectreference.returntypenonstaticmethod();
System.out.println("value is"+varaiblename);
(or)
how to call returntype nonstatic method in printstatement?
Ans)System.out.println("value is"+objectreference.returntypenonstaticmethod());
=================================================
Static Methods | Non-Static Methods |
These methods support early or compile-time binding. | They support late, run-time, or dynamic binding. |
These methods can only access static variables of other classes as well as their own class. | They can access both static as well as non-static members. |
You can’t override static methods. | They can be overridden. |
Less memory consumption since they are allocated memory only once when the class is being loaded. | Memories are allocated for each object. |
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.