Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8783431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:49:47+00:00 2026-06-13T20:49:47+00:00

/** * this is my class, yadayada. Needs to be subclassed to be useful.

  • 0
/**
 * this is my class, yadayada. Needs to be subclassed to be useful.
 */
public abstract class MyClass {
  ...
  public static void myMethod() {
    // What is the name of the subclass that's using this?
    System.out.println("Called from class " + this.getClass().class.getName());
    // Doesn't work because there's no "this" in a static method.
  }
}

Edit: this is what I want to happen:

public class FooClass extends MyClass {
  ...
}

FooClass.myMethod();   // I would like this to print "Called from class FooClass"

I’m all out of ideas.

Edit: And for those who wonder why I’m doing it this way, it’s actually an Android question of sorts.

Android defines a class called BroadcastReceiver. You subclass this and register your subclass with the system. When an appropriate broadcast is sent, the system creates an instance of your subclass and calls one of its methods.

I want to create my own BroadcastReceiver subclass that contains a static method for registering itself. But I also want it to be subclassable. It sort of looks like this:

/**
 * My special-purpose BroadcastReceiver.  Subclass this to use it.
 */
public class MyClass extends BroadcastReceiver {
  public static scheduleAnAlarm(long when) {
    // do some class-specific stuff
    AlarmManager.scheduleBroadcast(MyClass.class, when);
  }

  public void onBroadcast() {
    System.out.println("You should subclass MyClass for it to be useful");
  }
}

My problem is that I want to register the subclass to be instantiated, not MyClass.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T20:49:48+00:00Added an answer on June 13, 2026 at 8:49 pm

    You can’t. That is you cannot get the class of a subclass from a static method.

    When Java invokes a static method on a class (even one that is abstract) no subclass is required therefore none is loaded by the class loader. Therefore there is not necessarily any subclass to get. Even if there was, there could be multiple subclasses so how could Java decide which one you want?

    Edit, even with your edit you can’t get the subclass. There is a trick you can use with exceptions to get the calling class. You can throw / catch an exception and then walk down the stack trace one level to get the calling method / class. Not recommended!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given this class: public class MyClass { private void MyMethod() { //... } public
Consider this class: public class TestMap extends HashMap<String, Float> { public static void main(String[]
This is my abstract base class: public abstract class BaseDataModel<T> : System.IComparable<T> where T
//this class (or interface if you like) is set up as generic... public abstract
Imagine this: Class A{ private int a; } Class B{ public void fn(){ A
Given this class with an implicit cast operator: public class MyDateTime { public static
Consider this: class User < ActiveRecord::Base # name, email, password end class Article <
Given this class public partial class Default : Page { private IRepository repo; ...
given this class definition: public class Frame { IFrameStream CapturedFrom; } I want implement
Take this class as example: public class Category : PersistentObject<int> { public virtual string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.