Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: What java class am I in?  (Read 5385 times)

0 Members and 1 Guest are viewing this topic.

zscipio

    Topic Starter


    Rookie

    What java class am I in?
    « on: March 14, 2009, 10:27:19 PM »
    How can I find out at run time what java class I am in. Or what java class called me?

    thank you.

    TChai



      Rookie

      Thanked: 4
      Re: What java class am I in?
      « Reply #1 on: March 19, 2009, 11:19:02 AM »
      ???

      In every single class (code) you are in just write a code that will output the informations that is related to the class
      Easy i dont know why you want to do that?

      BC_Programmer


        Mastermind
      • Typing is no substitute for thinking.
      • Thanked: 1140
        • Yes
        • Yes
        • BC-Programming.com
      • Certifications: List
      • Computer: Specs
      • Experience: Beginner
      • OS: Windows 11
      Re: What java class am I in?
      « Reply #2 on: March 19, 2009, 11:39:26 AM »
      use reflection.
      I was trying to dereference Null Pointers before it was cool.

      zscipio

        Topic Starter


        Rookie

        Re: What java class am I in?
        « Reply #3 on: March 22, 2009, 05:10:54 PM »
        Thanks for the replies.  Let me explain the actual problem.  I have a main method which reads a file and passes the data to a class whose name  is a state code.  ie AZ, DE,MA etc.  Each of these state classes write out data that uses the state code.  Each of these state classes is coded by a different person and currently they each have a statement in them that reads:

        theState = "xx";  where xx is the name of the class.  What I am trying to avoid is one of the programmers using the wrong state code when it is just the name of the class.

        what I have come up with is the following method:

        private static String myName()
        {
              String className;
              int i = 0;
              className = "A";
                try
                {
                   int ii = 1 / i;
                }
                catch (Exception e)
                {
                    StackTraceElement[] se = e.getStackTrace();
                    String fullClassName = se[1].getClassName();
                    int lastDot = fullClassName.lastIndexOf(".");
                    className = fullClassName.substring(++lastDot);
                }
              return className;
        }


        Is there a simpler way of doing this.   I look up reflection but couldn't figure out how to use it.

        Thanks for any help.
         

        BC_Programmer


          Mastermind
        • Typing is no substitute for thinking.
        • Thanked: 1140
          • Yes
          • Yes
          • BC-Programming.com
        • Certifications: List
        • Computer: Specs
        • Experience: Beginner
        • OS: Windows 11
        Re: What java class am I in?
        « Reply #4 on: March 22, 2009, 05:24:33 PM »
        what about this.getClassName() instead of purposely causing an error, getting a stack trace and grabbing the top object?

        or am I missing something?
        I was trying to dereference Null Pointers before it was cool.

        zscipio

          Topic Starter


          Rookie

          Re: What java class am I in?
          « Reply #5 on: March 23, 2009, 09:44:59 AM »
          this.getClassName() will not compile.  I got it to compile.  Thanks everyone.
          « Last Edit: March 23, 2009, 03:47:51 PM by zscipio »