Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #37178

Re: Using main from abstract class in concrete class

From Cecil Westerhof <Cecil@decebal.nl>
Newsgroups comp.lang.java.programmer
Subject Re: Using main from abstract class in concrete class
Date 2017-08-13 15:43 +0200
Organization Decebal Computing
Message-ID <871sofwpl8.fsf@munus.decebal.nl> (permalink)
References <87h8xbx0ig.fsf@munus.decebal.nl> <new-Main-20170813112247@ram.dialup.fu-berlin.de>

Show all headers | View raw


ram@zedat.fu-berlin.de (Stefan Ram) writes:

> Cecil Westerhof <Cecil@decebal.nl> 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

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Using main from abstract class in concrete class Cecil Westerhof <Cecil@decebal.nl> - 2017-08-13 11:47 +0200
  Re: Using main from abstract class in concrete class Andreas Leitgeb <avl@logic.at> - 2017-08-13 10:59 +0000
    Re: Using main from abstract class in concrete class Andreas Leitgeb <avl@logic.at> - 2017-08-13 11:38 +0000
  Re: Using main from abstract class in concrete class Cecil Westerhof <Cecil@decebal.nl> - 2017-08-13 15:43 +0200
  Re: Using main from abstract class in concrete class Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-08-13 10:32 -0400
    Re: Using main from abstract class in concrete class Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-08-13 10:37 -0400
    Re: Using main from abstract class in concrete class Cecil Westerhof <Cecil@decebal.nl> - 2017-08-13 17:17 +0200
      Re: Using main from abstract class in concrete class Andreas Leitgeb <avl@logic.at> - 2017-08-17 18:56 +0000
        Re: Using main from abstract class in concrete class Cecil Westerhof <Cecil@decebal.nl> - 2017-08-18 06:37 +0200
          Re: Using main from abstract class in concrete class Andreas Leitgeb <avl@logic.at> - 2017-08-18 19:48 +0000
            Re: Using main from abstract class in concrete class Cecil Westerhof <Cecil@decebal.nl> - 2017-08-18 23:37 +0200
              Re: Using main from abstract class in concrete class Cecil Westerhof <Cecil@decebal.nl> - 2017-08-19 01:32 +0200
  Re: Using main from abstract class in concrete class Joshua Cranmer 🐧 <Pidgeot18@verizon.invalid> - 2017-08-13 12:03 -0500

csiph-web