Results 1 to 9 of 9
  1. #1
    gtriant is offline Member
    Join Date
    Mar 2008
    Posts
    6
    Rep Power
    0

    Default Read my class name from within static method

    Hello all,

    Suppose I have a regular public class and a static method within it, as in the example bellow.

    Java Code:
    package mypackage;
    
    import CommonResourceUtil;
    
    public class MyClass {
    	public static String myMethod(String input){
    		String response = CommonResourceUtil.getSomethingBecauseIamInThisClass(???);
    		return response;
    	}
    }
    How can I elicit the class name I am in in order to use it, without actually needing to "say the word" MyClass?
    Java Code:
    this.
    from a static context is not allowed. Any other way?

    Thank you

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default Re: Read my class name from within static method

    You're in a static context, so why not use MyClass.class?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    gtriant is offline Member
    Join Date
    Mar 2008
    Posts
    6
    Rep Power
    0

    Default Re: Read my class name from within static method

    Because I wont be able to be "moving around" methods without changing them at all, at the same time changing the functionality :)

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,382
    Rep Power
    5

    Default Re: Read my class name from within static method

    Can you please provide an example of how you want to move around methods and how this relates to determining their enclosing class in a static context?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    gtriant is offline Member
    Join Date
    Mar 2008
    Posts
    6
    Rep Power
    0

    Default Re: Read my class name from within static method

    For example:

    Java Code:
    public class CommonResourceUtil{
    
    	public String getSomethingBecauseIamInThisClass(Class C){
    		if(C instance of MyClass){
    			return "get something in relation to MyClass";
    		}
    	}
    
    }
    If I was able for something like that, then I would be able to move around the method myMethod in an other MyOtherClass
    But I don't think that the intention is of any relevance.

    Thanks

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default Re: Read my class name from within static method

    But if you're copy/pasteing it then may as well copy paste the name of the class in.
    In short, no...there is no 'this' equivalent in a static environment.

    I suspect you really want to not be using a static method there...but not knowing the why's and wherefore's that's just gut instinct at work.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,382
    Rep Power
    5

    Default Re: Read my class name from within static method

    I assume the "content" of something you are getting is going to be different based on the class. But the type must be the same (methods can't alter their return type from call to call). So it sounds to be like you could use an interface where each class implements the interface specifically for their class.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  8. #8
    gtriant is offline Member
    Join Date
    Mar 2008
    Posts
    6
    Rep Power
    0

    Default Re: Read my class name from within static method

    Thank you very much for your replies.

    FYI the context is several DB adapters classes (a different adaptor for different DB-connection pool) and the common resource is a "dynamic" connection pool handler.
    So I would like to keep a "model" of the retrieves methods intact throughout the several different adapters classes, using the method "getConnection(ThisAdapterClass)" from my resource class by passing the classname or class instance as an argument (so that things are more pretty and uniform).

    The retrieve methods in this context should be static, but I guess I will have to specify the class name explicitly when calling for a connection. No harm!

    Thanks a lot.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default Re: Read my class name from within static method

    Just looks to me like making this all static is the mistake...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. call a method from another class without static?
    By pancake718 in forum New To Java
    Replies: 2
    Last Post: 11-26-2011, 01:39 AM
  2. Replies: 6
    Last Post: 02-12-2011, 06:56 AM
  3. Replies: 3
    Last Post: 02-09-2010, 05:22 AM
  4. static method within same package not read
    By jon80 in forum New To Java
    Replies: 11
    Last Post: 06-15-2009, 09:47 AM
  5. Replies: 1
    Last Post: 02-10-2009, 10:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •