Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we create public static abstract class in java?

Tags:

java

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 ?

like image 916
Yasir Khan Avatar asked Sep 13 '12 09:09

Yasir Khan


People also ask

Can we create static abstract class?

Yes, of course you can define the static method in abstract class. you can call that static method by using abstract class,or by using child class who extends the abstract class. Also you can able to call static method through child class instance/object.

Can we have public static class in Java?

Classes can be static which most developers are aware of, henceforth some classes can be made static in Java. Java supports Static Instance Variables, Static Methods, Static Block, and Static Classes. The class in which the nested class is defined is known as the Outer Class.

Can we use public and abstract together in Java?

Only public & abstract are permitted in combination to method. Example: public abstract void sum(); We use abstract keyword on method because Abstract methods do not specify a body.

How do you create a public abstract class in Java?

To create an abstract class, just use the abstract keyword before the class keyword, in the class declaration. You can observe that except abstract methods the Employee class is same as normal class in Java. The class is now abstract, but it still has three fields, seven methods, and one constructor.


1 Answers

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 = ...
like image 60
David Rabinowitz Avatar answered Oct 12 '22 23:10

David Rabinowitz