19

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,664
  • 11
  • 59
  • 87
Yasir Khan
  • 2,413
  • 4
  • 20
  • 26

2 Answers2

27

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,432
  • 1
  • 17
  • 20
David Rabinowitz
  • 29,904
  • 14
  • 93
  • 125
4

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
  • 10,116
  • 6
  • 42
  • 76