Asked  7 Months ago    Answers:  5   Viewed   50 times

I was just reading over the text given to me in my textbook and I'm not really sure I understand what it is saying. It's basically telling me that static methods or class methods include the "modifier" keyword static. But I don't really know what that means?

Could someone please explain to me in really simple terms what Static or Class Methods are?

Also, could I get a simple explanation on what Instance methods are?

This is what they give me in the textbook:

There are important practical implications of the presence or absence of the static modifier. A public class method may be invoked and executed as soon as Java processes the definition of the class to which it belongs. That is not the case for an instance method. Before a public instance method may be invoked and executed, an instance must be created of the class to which it belongs. To use a public class method, you just need the class. On the other hand, before you can use a public instance method you must have an instance of the class.

The manner in which a static method is invoked within the definition of another method varies according to whether or not the two methods belong to the same class. In the example above, factorial and main are both methods of the MainClass class. As a result, the invocation of factorial in the definition of main simply references the method name, "factorial".



The basic paradigm in Java is that you write classes, and that those classes are instantiated. Instantiated objects (an instance of a class) have attributes associated with them (member variables) that affect their behavior; when the instance has its method executed it will refer to these variables.

However, all objects of a particular type might have behavior that is not dependent at all on member variables; these methods are best made static. By being static, no instance of the class is required to run the method.

You can do this to execute a static method:

MyClass.staticMethod();  // Simply refers to the class's static code

But to execute a non-static method, you must do this:

MyClass obj = new MyClass();//Create an instance
obj.nonstaticMethod();  // Refer to the instance's class's code

On a deeper level the compiler, when it puts a class together, collects pointers to methods and attaches them to the class. When those methods are executed it follows the pointers and executes the code at the far end. If a class is instantiated, the created object contains a pointer to the "virtual method table", which points to the methods to be called for that particular class in the inheritance hierarchy. However, if the method is static, no "virtual method table" is needed: all calls to that method go to the exact same place in memory to execute the exact same code. For that reason, in high-performance systems it's better to use a static method if you are not reliant on instance variables.

Tuesday, June 1, 2021
answered 7 Months ago

You can find these settings in the Django documentation. Here are my own definitions and quotations from the documentation:

  • MEDIA_ROOT is the folder where files uploaded using FileField will go.

    Absolute filesystem path to the directory that will hold user-uploaded files.

  • STATIC_ROOT is the folder where static files will be stored after using collectstatic

    The absolute path to the directory where collectstatic will collect static files for deployment.

    If the staticfiles contrib app is enabled (default) the collectstatic management command will collect static files into this directory. See the howto on managing static files for more details about usage.

  • STATICFILES_DIRS is the list of folders where Django will search for additional static files aside from the static folder of each app installed.

    This setting defines the additional locations the staticfiles app will traverse if the FileSystemFinder finder is enabled, e.g. if you use the collectstatic or findstatic management command or use the static file serving view.

In your settings, you should have:

MEDIA_ROOT = os.path.join(BASE_DIR, "media/")
STATIC_ROOT = os.path.join(BASE_DIR, "static/")

# Make a tuple of strings instead of a string
STATICFILES_DIRS = ("/home/user/project/django1/top/listing/static", )


BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

as defined in the default Django now.

Saturday, June 5, 2021
answered 7 Months ago

The lock objects are different on the static method and non-static method. The static method uses the Class object as the lock (lock obj: MyClass.class), while the non-static method uses the instance object as the lock to which the invocation of the method at that time is bound (lock obj: this).

Thursday, July 29, 2021
answered 5 Months ago

All the other answers seem to have been caught out by the incorrect tag that has now been fixed.

In Objective-C, an instance method is a method that is invoked when a message is sent to an instance of a class. So, for instance:

id foo = [[MyClass alloc] init];
[foo someMethod];
//   ^^^^^^^^^^   This message invokes an instance method.

In Objective-C, classes are themselves objects and a class method is simply a method that is invoked when a message is sent to a class object. i.e.

[MyClass someMethod];
//       ^^^^^^^^^^   This message invokes a class method.

Note that, in the above examples the selector is the same, but because in one case it is sent to an instance of MyClass and in the other case it is sent to MyClass, different methods are invoked. In the interface declaration, you might see:

@interface MyClass : NSObject

+(id) someMethod;  // declaration of class method
-(id) someMethod;  // declaration of instance method


and in the implementation

@implementation MyClass

+(id) someMethod
    // Here self is the class object
-(id) someMethod
    // here self is an instance of the class



Sorry, missed out the second part. There are no advantages or disadvantages as such. It would be like asking what is the difference between while and if and what are the advantages of one over the other. It's sort of meaningless because they are designed for different purposes.

The most common use of class methods is to obtain an instance when you need one. +alloc is a class method which gives you a new uninitialised instance. NSString has loads of class methods to give you new strings, e.g. +stringWithForma

Another common use is to obtain a singleton e.g.

+(MyClass*) myUniqueObject
    static MyUniqueObject* theObject = nil;
    if (theObject == nil)
        theObject  = [[MyClass alloc] init];
    return theObject;

The above method would also work as an instance method, since theObject is static. However, the semantics are clearer if you make it a class method and you don't have to first create an instance.

Friday, August 20, 2021
answered 4 Months ago

I want this method to be usefull with an object and also without it. Is it possible to do something like that without creating another method?

You will have to create another method, but you can make the non-static method call the static method, so that you do not duplicate the code and if you want to change the logic in the future you only need to do it in one place.

public class Test {
    private int a;
    private int b;
    private int c;

    public Test(int a, int b, int c) {
        this.a = a;
        this.b = b;
        this.c = c;

    public String count() {
        return count(a, b, c);

    public static String count(int a1, int b1, int c1) {
        String solution;
        solution = Integer.toString(a1 + b1 + c1);
        return solution;

    public static void main(String[] args) {
        System.out.println(Test.count(1, 2, 3));
        Test t1 = new Test(1, 2, 3);
Monday, August 23, 2021
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :