Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Java's 'self' keyword

Tags:

java

oop

php

I'm trying to write a factory method in an abstract class in Java (so I want it to return a new instance of the extending class, rather than the super-class).

In PHP I'd do this using the self keyword:

abstract class Superclass {
    public static function factory($arg) {
        return new self($arg);
    }

    private function __construct($arg) {}

    abstract public function doSomething() {}
}

Does Java have a keyword like self I can use for this?

like image 373
Ross Avatar asked May 26 '11 22:05

Ross


People also ask

What is Self in Delphi?

Description. In every method, Delphi declares the Self variable as a hidden parameter. In a method, the value of the Self variable is the object reference. In a class method, Self is the class reference.

Is self similar to this keyword?

Technically both self and this are used for the same thing. They are used to access the variable associated with the current instance. Only difference is, you have to include self explicitly as first parameter to an instance method in Python, whereas this is not the case with Java.

What is self C#?

To refer to the current instance of a class, use the this keyword. The this keyword provides a way to refer to the specific instance in which the code is currently executing. It is particularly useful for passing information about the currently executing instance.

What is self PHP?

self is used to access static or class variables or methods and this is used to access non-static or object variables or methods. So use self when there is a need to access something which belongs to a class and use $this when there is a need to access a property belonging to the object of the class.


1 Answers

No; in Java, static methods are not inherited in the same way as non-static methods are. A subclass will have the static methods of its superclass, but when they execute, they will execute in context of the superclass - so there is no keyword that can be used in static methods to find out what class the method was invoked through.

Edit: A more precise formulation is that static methods are not inherited at all; however, the language allows us to use Subclass.foo() to call the static method Superclass.foo().

Based on what you seem to want to achieve, you might want to implement the Abstract Factory pattern. It goes approximately like this:

public abstract class Superclass {}
public class SubclassA extends Superclass {}
public class SubclassB extends Superclass {}

public abstract class AbstractFactory {
    public abstract Superclass Create();
}

public class FactoryA extends AbstractFactory {
    public Superclass Create() {
        return new SubclassA();
    }
}

public class FactoryB extends AbstractFactory {
    public Superclass Create() {
        return new SubclassB();
    }
}

Now, you can e.g. create a method that takes an AbstractFactory (which, in reality, will be either a FactoryA or a FactoryB). Calling Create() on this object will produce either a SubclassA or a SubclassB.

Edit: Fixed compilation error (forgot to make the factories extend AbstractFactory).

like image 56
Aasmund Eldhuset Avatar answered Sep 23 '22 18:09

Aasmund Eldhuset