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 3875118
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T22:13:47+00:00 2026-05-19T22:13:47+00:00

In Java, I want to create a few geometric shapes based on user input.

  • 0

In Java, I want to create a few geometric shapes based on user input. The trick is that I can’t change the existing API, so I can’t use varargs syntax like

public Shape(Object... attrs) {}

User input:

shape.1.triangle.arg1 = 3
shape.1.triangle.arg2 = 4
shape.1.triangle.arg3 = 5
shape.1.triangle.arg4 = "My first triangle"
shape.2.rectangle.arg1 = 4
shape.2.rectangle.arg2 = 7
shape.2.rectangle.arg3 = "Another string label"

Should lead to method invocations like:

Shape s1 = new Triangle(arg1, arg2, arg3, arg4);

Or generically to:

String shapeType = "triangle";
Object[] args = {arg1, arg2, arg3, arg4};
// This won't work, because newInstance() doesn't take args
Shape s1 = Class.forName(shapeType).newInstance(args); 

String shapeType = "rectangle";
Object[] args = {arg1, arg2, arg3};
// This won't work, because newInstance() doesn't take args
Shape s2 = Class.forName(shapeType).newInstance(args);

The problem is that the constructor for Triangle does not allow varargs (…), and I can’t change it. Specifically, the constructors are

public Triangle (int a, int b, int c, String label) {}

public Rectangle (int a, int b, String label) {}

So how do I create the right shapes based on user input?

  • 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-19T22:13:48+00:00Added an answer on May 19, 2026 at 10:13 pm

    This is an interesting approach. The problem is not that Triangle lacks a vararg constructor, but rather the fact than newInstance() can only invoke the default constructor.

    mschaef has proposed a reasonable approach. It does still require a static awareness of the constructor forms of each shape. This knowledge will then be hidden in the respective factories. It is probably the best solution. The problem is that you have to write a factory for each shape.

    However, you can write code that dynamically invokes the correct constructor using the reflection APIs. If you have lots of shapes, that will solve the problem in one place for all of them. You would obtain the Class object, and then call getDeclaredConstructors() to obtain an array of constructors. Constructor argument types can be queried by getParameterTypes(). Your code would have to find the ideal constructor based on the parameters you have. This is essentially what the compiler would have done with static types.

    This solution is not particularly elegant, but it does have plus points:
    If you write it correctly once, it will always work for any new shape that you might introduce. You can place the code in a neat utility class and never look at it again.

    In general, I still consider using reflection as an undesirable choice.

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

Sidebar

Related Questions

I want to create a Java counter that counts the amount of wins a
Say I want to create a sort of Pre-processor for existing java code, so
I want to create an SSH Tunnel in Java. I noticed quite a few
I want create a excel with Apache POI in java and I must insert
I am making a game in java and I want to create a simulation
I want to create a Java Desktop Application and want to employ some mechanism
I want to create registry key through java program to add the jar file
I want to create a capped collection from Java code. I found the syntax
I want to create a simple bash script to launch a Java program on
I want to create a purpose built device capable of running a java vm

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.