Java is statically Typed
1.In Java ,Need to specify the "return" ,"not Return"
"void" means not return anything
public class Shop
{
Static String ShopName = "Priya"
String name;
int price;
public static void main(String[]args)
{
Shop product1 = new Shop()
product1.name = "abc";
product1.price = 10;
product1.buy();
}
}
Output
Cannot find symbol
Explanation:
If method is not declared means [Cannot Find Symbol]
[Invalid Method Declaration]
If method is not Correctly 'defined' and 'Return Type' is not given
Inside a method ,we can call another method .
Inside a method ,we cannot define it
Using Void
public class Shop
{
Static String ShopName = "Priya"
String name;
int price;
public static void main(String[]args)
{
Shop product1 = new Shop()
product1.name = "abc";
product1.price = 10;
product1.buy();
}
public void buy(){
System.out.println("Buy Method")
}
output
Buy Method
public class Shop
{
static String ShopName = "Priya";
String name;
int price;
void buy()
{
System.out.println("Product: " + name);
System.out.println("Price: " + price);
System.out.println("Shop: " + ShopName);
}
public static void main(String[] args)
{
Shop product1 = new Shop();
product1.name = "abc";
product1.price = 10;
product1.buy();
}
}
Output
Product: abc
Price: 10
Shop: Priya
public class Calculator
{
public static void main(String[] args)
{
Calculator casio = new Calculator();
casio.add(10, 20);//method calling
}
//method definition
public void add(int no1, int no2)
{
System.out.println(no1 + no2);
}
}
Output
30
Top comments (0)