Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Get static method to know which class it was called on

  1. #1
    Junior Member
    Join Date
    Oct 2019
    Location
    Norway
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Get static method to know which class it was called on

    I am creating models, and in the base class for models, I need a static method that returns an instance of the class that the method was called on. As an example, when Person.getInstance() is called, I want new Person() to be returned.

    The problem for me is to find out what class the function was called on. Any suggestions?

    /**
     * The base class for models
     */
    public class BaseModel {
     
        public static BaseModel getInstance() {
            // I want this method to return an instance of the class that it was
            // called on, like Person or Vehicle.            
     
        }
     
    }
     
    public class Person extends BaseModel {
     
        public String name;
     
        public Person() {
            this.name = "John Doe";
        }
    }

  2. #2
    Junior Member
    Join Date
    Sep 2019
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Get static method to know which class it was called on

    Not simple. If you know all possible Classes that extend BaseModel, you can test with the instanceof all possible Cases.
    For Example:
     if (this instanceof Person) ...

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Get static method to know which class it was called on

    In Person:
    public static Person getInstance() {
       return new Person();
    }

    I don't know if what you want can be done.

    Try asking on the Stack Overflow site.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Oct 2019
    Location
    Norway
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Get static method to know which class it was called on

    Quote Originally Posted by Norm View Post
    In Person:
    public static Person getInstance() {
       return new Person();
    }

    I don't know if what you want can be done.

    Try asking on the Stack Overflow site.
    I want the static function in the base class to return an instance of the class the function was called from. That function is intended to include a lot more code, and it would be greate to have just one function that did the job for all models inheriting the base.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Get static method to know which class it was called on

    Did you try asking on StackOverflow? There are a lot of more experienced programmers there.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 7
    Last Post: March 3rd, 2019, 08:44 PM
  2. Call static method from another class - SPMF library
    By davi-d in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 12th, 2019, 04:30 PM
  3. How to call a static method within a static method in the same class?
    By EDale in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 10th, 2013, 04:13 AM
  4. static method in super class
    By elim in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 4th, 2013, 07:55 PM
  5. update static field and call method within the class?
    By GeneralPihota in forum Object Oriented Programming
    Replies: 7
    Last Post: February 6th, 2012, 09:20 PM

Tags for this Thread