0

I am writing an administrator class that all other classes I am writing are meant to inherit from. In this case I want the classes to inherit a main method, and I am planning on implementing a factory as well. There are a few sysouts that I have to write that include various class names such as charcount, linecount and the such. For this I need to be able to get the current class name, since this method will be inherited by multiple classes. Is there a way to do this in java? You can find an example of the sysout I am looking to do below:

System.out.println("Usage: className <src>\n"); 

Thank you all in advance, any help is appreciated!

edit: Hey guys!

Very sorry I wasnt clear before, I'll show the entire code below and try to further explain my idea. So what I am doing is essentially making a main method that checks to see if a file exists, and if the file exists, it will try to execute a certain function (All this will be in the command line).I am practicing factory design patterns (something I've never done before). I will use the execute part of the main method to call on some factory (not yet made) to create objects of subclasses of administrator and return things like charcount in a file and linecount in a file. The above question refers to one part of this main method that will print the usage of the subclass. So for example, I will have charcount inherit from administrator because the process of checking the file and is in the main method of administrator, however when I print the usage message, I dont want it to print "Usage: Administrator ", but "Usage: charcount ". I hope that clears things up, and sorry again for how badly I explained it above, you can find what I have so far as code below:

public class Administrator { private static File srcFile = null; private static String srcFilename = "<srcFilename>"; public static void main(String[] args) throws IOException { //check the command line if(args.length != 1) { System.out.println("Usage: charcount <src>\n"); return; } // Check if arguments are valid, if the srcFile exists, and if can create the dstFile. if (args[0] != null) { //check <src> srcFilename = args[0]; System.out.println( ": srcFilename '" + srcFilename + "'"); srcFile = new File(srcFilename); if(!srcFile.canRead()) { System.out.println("charcount: cannot open srcFile '" + srcFilename + "'"); return; } } else { System.out.println("charcount: [OK] srcFilename = '" + srcFilename + "'"); } } public static int execute() {return 0;} } 
3
  • 1
    Please, provide what you have done so far, and explain what exactly you want to implement. Generally, object.getClass().getName(); returns the name of the class of object. Commented Sep 28, 2020 at 13:16
  • Is "this method" an instance method or a static one? Commented Sep 28, 2020 at 13:18
  • it is a static one, the idea is that the main method is very similar on all my classes except the execution part, so what I am planning is writing a different execution for each of the classes then passing the fileName to that execution program and displaying the results. Commented Sep 28, 2020 at 13:43

3 Answers 3

1

You can do something like this:

System.out.println("Class name: " + getClass().getName());

Which will return foo.bar.Baz (assuming your class is named Baz and in package foo.bar)

Or you can System.out.println("Class name: " + getClass().getSimpleName());

Which will simply return 'Foo'

You might find this related question helpful: What is the difference between canonical name, simple name and class name in Java Class?

Edit: The question asks about doing this from the "main" method, which could be understood as public static void main(String[] args) or as the primary method in a given class hierarchy. Since the question goes on to discuss the method being inherited by multiple classes, I understood "main" as "primary". This answer wouldn't work from public static void main(String[] args) because getClass() is an instance method and can't be invoked from a static context.

Sign up to request clarification or add additional context in comments.

Comments

0

Java Class class has some useful methods. You can use getClass().getSimpleName(). at runtime this method called based on object's type.

1 Comment

You can't use that in a static method, and OP talks about a main method (typically being static), so it's quite likely this doesn't solve the OP's problem.
0

Try using this methods:

this.getClass().getCanonicalName() or this.getClass().getSimpleName().

If it's an anonymous class, you should use this.getClass().getSuperclass().getName()

2 Comments

You can't use that in a static method, and OP talks about a main method (typically being static), so it's quite likely this doesn't solve the OP's problem.
I understood that author wants to create Admin.class and then to inherit sub-classes from this class. So he could use these methods somewhere in toString() or customGetClass() method

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.