- Create a class called Cricket
- Create static variable int overs = 20;
- Create non-static variable int score;
- Have main method in it.
- Inside main method, create two instances dhoni, kohli.
- For dhoni, assign score as 50; dhoni.score = 50;
- For kohli, assign score as 60;
- Create a method (outside main method) as below. public void display() { System.out.println(score); System.out.println(overs); }
- Now, using object dhoni call display() method. dhoni.display()
- Using instance kohli, call display() method. kohli.display()
Input:
class cricket
{
static int overs = 20;
int score;
public static void main(String[] args)
{
cricket dhoni = new cricket();
dhoni.score = 50;
cricket kohli = new cricket();
kohli.score = 60;
kohli.display();
}
public void display()
{
System.out.println("kohli"+ score);
System.out.println("kohli"+ overs);
}
}
output:
Top comments (0)