Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 444395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T21:15:58+00:00 2026-05-12T21:15:58+00:00

How do I self-instantiate a Java class? I should not use any file other

  • 0

How do I self-instantiate a Java class? I should not use any file other than its class file. Also assume that after the line where I initialized the NAME variable, they don’t know the name of the class during compile-time.

Example:

public class Foo implements Bar {
    public static final String NAME = "Foo";

    private void instantiate()  {
        Bar baz = (Bar)Class.forName(NAME).newInstance();
    }
    //...
}

would be wrong since it needs to have another file called Bar.java. It should be:

public class Foo {
    /*
    Instantiate self...
    */
}

I joined this programming challenges website where I am only allowed to submit one file per problem. I want to create a template class for all the classes that I’ll be using for the challenges. So, what I have in mind is that I’ll do this:

public class Foo {
    public static final String NAME = "Foo";


    public static void main (String[] args) throws IOException {
        //Instantiate self and call launch(args)
        Foo foo = new Foo();
        foo.launch(args);
        System.exit(0);
    }

    public void launch(String[] args) {
        //...Do actual code here
    }
}

I want to only change the class name and the NAME variable and not have to change the way I instantiate the class everytime I use the template class. With the current setup I have, I’ll have to edit the line: Foo foo = new Foo(); and maybe the line below that.

You might also be wondering why I have to call launch() and not do everything inside the main method. That’s just something I got from my Java instructor. It’s also since I can’t use non-static methods inside the main() method.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T21:15:59+00:00Added an answer on May 12, 2026 at 9:15 pm

    If you “implement” anything, you NEED to have an interface defined. Either you import one that already exists, or you have to create it. Either way, if it’s a public class (abstract or not) or an interface, it MUST be defined in a separate file. Unlike C++, this is a java requirement.

    If:

    public class Foo implements Bar { 
    } 
    

    then your project must be comprised of at least:

    Foo.java
    Bar.java 
    

    Period. No ifs/ands/buts.

    But if you absolutely must have only one file in your project, you’ll have to get a byte-code generator and build your class and interfaces dynamically at runtime.

    The real question is: what are you trying to accomplish? If it’s a Singleton/Factory, others have answered that already. If it’s to reduce the number of compilation units, then you’re out of luck. Java enforces the requirement that you have to do it this way.

    EDIT AFTER AUTHOR UPDATES:

    Java lets you do things like so:

    Foo.java:

    public class Foo { 
      public static void main(String[] args) { 
        Bar bar = new Bar(); 
      }
    }
    class Bar { 
       int bar_var; 
       Snafu fu = new Snafu(); 
    
       public Bar() {  } 
       public int getBarVar() { return bar_var; } 
    } 
    class Snafu { 
       int var; 
       public Snafu() { } 
    }
    
    .... 
    

    Which would accomplish what you want to achieve.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 278k
  • Answers 278k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Most likely, PHP and ActionScript are using different encodings for… May 13, 2026 at 3:14 pm
  • Editorial Team
    Editorial Team added an answer You should url-encode all the values you are passing as… May 13, 2026 at 3:14 pm
  • Editorial Team
    Editorial Team added an answer Yes, you can use them together. And yes, it should… May 13, 2026 at 3:14 pm

Related Questions

I know there is no concept of an abstract class in Ruby. But if
I recently started a C# project (VS 2008) as a 'Console' project where I
I need to import some data from a excel file and a folder with
so i'm trying to do a data migration where i take the listings from

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.