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

  • Home
  • SEARCH
  • 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 1096055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:13:29+00:00 2026-05-17T00:13:29+00:00

I am newbie to java programming language. My problem is: I want to read

  • 0

I am newbie to java programming language.

My problem is:
I want to read sys.input for a class name from console. Upon reading the name of the class, I want to generate that class automatically and call its method if that class exists already.
my trial is here. Although I dont get any error, nothing happens.
my kind regards.


class s1{
    public s1(){
        System.out.println(""+ s1.class);
    }
}
public class reflection {

    public static void main(String[] args) throws IOException, ClassNotFoundException{

        System.out.println("enter the class name : ");    
        BufferedReader reader= new BufferedReader(new InputStreamReader(System.in));
        String line = "reflection_N3."; 
        line+=reader.readLine();

        //System.out.println(line);

     // "name" is the class name to load
       Class clas = Class.forName(line);       
       clas.getClassLoader();     
    }
}
  • 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-17T00:13:29+00:00Added an answer on May 17, 2026 at 12:13 am

    You are not creating an instance of the class. Try

    Class clas = Class.forName(line);
    Object obj = clas.newInstance();
    

    However, the problem is, you can’t do much with this object unless you know its exact type, and cast it to that type.

    In this example, you could try casting it to your class type, e.g.

    if (obj instanceof s1) {
      s1 myS1 = (s1) obj;
      myS1.s1();
    }
    

    However, this hardly works in real life, where you don’t know the possible types in advance. The typical solution to this is to define an interface for a specific purpose, and require that the class implements that interface. Then you can downcast the class instance to that interface (throwing an exception if the cast fails), and call its interface methods, without needing to know its concrete type.

    Or, as @helios noted, you can use reflection to obtain a method of the loaded class having a specific name.

    Btw the Java convention is to start class names with uppercase, hence S1 and Reflection.

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

Sidebar

Related Questions

I'm a newbie in java and I have a small problem. I want to
Java newbie. I am trying to run a java main class from cmd line
I'm a newbie on Java EE and got a problem that I don't understand
Just a simple question from a relative Java newbie: what is the difference between
I am a newbie to AS400-Java programming. I am trying to create my first
I am a newbie to J2me. I am programming a Java program to recognise
I am a newbie to java script and currently reading John Resig's Pro javascript
I'm a relative newbie to Java and am building an application for my programming
newbie doing Java homework here. I have one class named Album which contains the
I'm a java newbie looking for some help reading a file. Here is the

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.