17

I was searching in google for something and I got a code like

public static abstract class LocationResult{
    public abstract void gotLocation(Location location);
}

It's a nested class but wondering how it could be accessible ?

akash
  • 22,224
  • 10
  • 57
  • 85
Yasir Khan
  • 2,353
  • 4
  • 18
  • 26

2 Answers2

25

It must be a nested class: the static keyword on the class (not methods within it) is only used (and syntactically valid) for nested classes. Such static member classes (to use Java in a Nutshell's common nomenculture) hold no reference to the enclosing class, and thus can only access static fields and methods within it (unlike non-static ones; see any summary of nested classes in Java (also known as inner classes).

It can be accessible like this:

public class EnclosingClass {
  public static abstract class LocationResult{
    public abstract void gotLocation(Location location);
  }
}

EnclosingClass.LocationResult locationResult = ...
Stuart Rossiter
  • 2,212
  • 1
  • 16
  • 19
David Rabinowitz
  • 28,996
  • 14
  • 92
  • 124
3

Only nested classes can be static. By doing so you can use the nested class without having an instance of the outer class.

So you could create a class extending it using extends Mainclass.LocationResult and use it with Mainclass.LocationResult instance = ...

Michael Laffargue
  • 9,947
  • 6
  • 40
  • 75