Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Cecil Westerhof Newsgroups: comp.lang.java.programmer Subject: Re: Using main from abstract class in concrete class Date: Sun, 13 Aug 2017 15:43:15 +0200 Organization: Decebal Computing Lines: 84 Message-ID: <871sofwpl8.fsf@munus.decebal.nl> References: <87h8xbx0ig.fsf@munus.decebal.nl> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: mx02.eternal-september.org; posting-host="1615173a1409002a85fbdd25ac4b9ead"; logging-data="31362"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/FKmCqYY6cHiNN0VDMV3StKH1eRCUr4wE=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux) Cancel-Lock: sha1:wQ5IXNaBufDNbqxDcAgdU+MZp2E= sha1:oN3mZ9fVsCo0dZ1dYef67f8KMsg= X-Homepage: http://www.decebal.nl/ Xref: csiph.com comp.lang.java.programmer:37178 ram@zedat.fu-berlin.de (Stefan Ram) writes: > Cecil Westerhof writes: >>I have an abstract class. There can be several concrete classes that >>extend this class. Is it possible to write a main in the abstract >>class which will be used by the concrete classes and that in the main >>a instance of the concrete class is created? > > The noun phrase »a main« does not have a general meaning in the > Java community. I assume you mean a method with the well-known > proclamation > > public static void main( final java.lang.String[] args ) Yes, that was what I mend. I should have been more clear. > > . Main.java > > abstract class Abstract > { > public static void main( final java.lang.String[] args ) > { try > { java.lang.Class.forName( args[ 0 ]). > getDeclaredConstructor().newInstance(); } > catch > ( final > java.lang.ClassNotFoundException | > java.lang.NoSuchMethodException | > java.lang.InstantiationException | > java.lang.IllegalAccessException | > java.lang.reflect.InvocationTargetException > exception ) > { java.lang.System.err.println( exception ); }}} > > public final class Main extends Abstract > { > > public Main() > { java.lang.System.out.println( "new Main()" ); } > > public static void main( final java.lang.String[] args ) > { final java.lang.String[] arguments = > { Main.class.getCanonicalName() }; > Abstract.main( arguments ); }} > > transcript > > new Main() > At the moment I have: public static void main(final String[] args) throws SQLException { SQL.mainWork(SQLite3.class.getCanonicalName(), args); } And in SQL.java I have: static void mainWork(final String className, final String[] args) throws SQLException { String action; String database; String[] parameters; if (args.length < 2) { System.err.println("Need at least two parameters: command and database."); System.exit(1); } // First argument is action // Second argument is database action = args[0]; database = args[1]; parameters = Arrays.copyOfRange(args, 2, args.length); try { SQL connection = (SQL) Class.forName(className) .getDeclaredConstructor(new Class[] {String.class}) .newInstance(database); switch (action) { case "--testConnection": -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof