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

The Archive Base Latest Questions

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

Can someone tell me what is the need to declare a class like this:

  • 0

Can someone tell me what is the need to declare a class like this:

public class Test {

 String k;
 public Test(String a, String b, String c){
  k = a + " " + b + " " + c; //do something

 }

 public void run(){
  System.out.println(k);
 }

 public static void main(String[] args) {
  String l = args[0];
  String m = args[1];
  String n = args[2];
  Test obj = new Test(l,m,n);
  obj.run();
 }

}

Of course it works but I don’t get the point why would one use such way to implement something. Is it because we need to pass arguments directly to the class main method that is why we use this way or is there some other reason?

What is the purpose of public Test(...) using the same class name. Why is it like this?

  • 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-18T06:15:12+00:00Added an answer on May 18, 2026 at 6:15 am

    The public Test(...) is a constructor and its purpose is for object creation. This is clearly seen from the sample code…

    Test obj = new Test(l,m,n);
    

    The variable obj is instantiated with object Test by being assigned to the Test‘s constructor. In java, every constructor must have the exact same name (and case) as the java file it’s written in (In your case constructor Test is found in Test.java).

    …Why is it like this?

    It all depends on what you want to do with your object. You could have a zero-argument constructor (i.e. requires no parameters) and have methods to set your l, m, n, like so:

    package net;
    
    
    public class Test {
    
        private String k;
    
        /**
         * 
         */
        public Test() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        public void set(String a, String b, String c) {
             k = a + " " + b + " " + c; //do something
        }
    
        public void run() {
            System.out.println(k);
        }
    
        public static void main(String[] args) {
            String l = args[0];
            String m = args[1];
            String n = args[2];
            Test obj = new Test();
            obj.set(l, m, n);
            obj.run();
        }
    }
    

    As you can see, it’s exactly the same feature as your example but with a zero-argument constructor.

    If your class has no constructor at all, java adds a public zero-argument constructor for you automatically.

    Hope this helps.

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

Sidebar

Related Questions

Can someone tell me how to change directories using FtpWebRequest? This seems like it
Can someone tell me how to modify this regex to allow periods in a
can someone tell me what seems to be the issue with this <?php $increment
Can someone tell me why this processes all the files and then does it
Can someone tell me why when I cast a string of say 00332 I
Can someone tell me why this http://hupcapstudios.com/projects/calendar.html won't load in IE? The error message:
Can someone tell me the best way to run a piece of JCL on
Can someone tell me what's going on here? It looks to me like myObj
This is really basic PHP. Can someone tell me why this does not work
Can someone tell me why this alert is empty? var pending_dates = []; $.getJSON('/ajax/event-json-output.php',

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.