Click here to Skip to main content
15,877,967 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Guys I dont know how to show the difference pls help

What I have tried:

I have made separate program of static and instant method but I don't know how to show difference please help
Updated 19-Sep-22 2:44am

Think about what the difference is: what makes a method static and what makes it have to be instance related.

Static methods can only access static fields, static properties, and static methods - they cannot access any non-static information at all.

Why not? What is the difference between static info and non-static info? Are there any similar features in the real world?
Think about that for a moment.

The answer is "yes". Think about a car for example - it has many properties: colour, number of wheels, make, model, registration number are just some of them. But not all cars are the same colour, are they? Colour is an instance property - it is specific to a particular car. "This car is green", "That car is red". Colour cannot be a static property of a car because that would mean every car would have to be the same colour.
And you can't say "what colour is a car?" because you need a specific car to find out what colour it is.

The same thing applies to make - not all cars are a Ford. Same for model, for registration number.

But ... all cars have four wheels - if they had two they would be a motorbike!
So "number of wheels" is a static property: you do not have to indicate a specific car in order to count it's wheels as all cars return "4".

Think about that, and see if you can work out a similar division in other aspects of the real world - there are massive numbers if you look at the problem!

Then pick one, and write yourself a simple class that needs two properties: a static and a non-static one. It shouldn't be difficult once you get the whole idea sorted in your head.
Good luck!
Share this answer
CPallini 16-Sep-22 2:40am    
Share this answer
// # -1. The instance method belong to object of a class not to class directly.
// # -example code

class Car{
String name;
static int num_of_tyres;

public void showName(String name){ // instance method to be called by object of class Car only;

public static void showTyres(int num_of_tyres){ //static method to be called directly by class name
System.out.println("number of tyres = : "+num_of_tyres);

class TestMethods{
public static void main(String []args){
Car c=new Car(); // object of Car created to access instance method showName()

Share this answer
Member 15772045 19-Sep-22 8:51am    
Here I have created two methods
1-INSTANCE method (ShowName) that requires the object of its class to be initialized
2- STATIC method (showTyres) that does not require an object of class to be initiazed.

if you need further clarification connect through linkedIn:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900