Hi,
I just wanted to know about instance method? If static modifier is not applied before does it makes it an instance method ? Plz give an example
Thanku

Recommended Answers

All 3 Replies

Static methods are NOT instance methods! An instance method will operate on instance variables typically, and require an instance to run. A class static method is like a global function, but associated more closely with a class, especially if the class has static member variables.

If static modifier is not applied before does it makes it an instance method ?

Yes.

commented: Sometimes the simplest answers are the best! +15

Below is a sample code snippet showing difference between static method and instance method.
Declaration
Static Method

class DetailsBean{
pubic static String setName(String name){
    this.name = name;
    }
}

Instance Method

class DetailsBean{
    pubic String setName(String name){
        this.name = name;
        }
    }

How to access

Static Method

public class User{
public void populateDetails(){
    DetailsBean.setName(name);
}
}

InstanceMethod

 public class User{
    public void populateDetails(){
        DetailsBean detailsBean = new DetailsBean();
        detailsBean.setName(name);
    }
 }

Hope it was helpful!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.