Question

The following question is about java programming. Is it true or false that one can send...

The following question is about java programming. Is it true or false that one can send in arguments
to a constructor?
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:
It is true that one can send arguments to a constructor. Actually, there are two types of constructors:

1. Without arguments: If we do not define a constructor for a class, then the compiler in JAVA creates a default constructor. Now, this default constructor is without any arguments. One thing to note here is that, if we do define a constructor for a class, then there's no creation of default constructor by the JAVA compiler.

2. With arguments: It is very simple to understand. With arguments means that we define a constructor for a class and this constructor is with arguments. We can pass various types of data to a constructor.

I have executed a program where I have sent the argument to a constructor. Here it is if you need it.

Program:

class constructor_with_arguments {

   private String str; // data member of class constructor_with_arguments

   // this is the constructor with arguments, it would receive an argument st
   // of
   // type String
   public constructor_with_arguments(String st) {

       this.str = st; // initializing str with the value of st

   }

   // this is a member function of class constructor_with_arguments
   public String display() {
       return this.str;
   }
}

public class Chegg {

   public static void main(String args[]) {

       // here we are sending the argument to the constructor
       constructor_with_arguments Obj = new constructor_with_arguments("Chegging");

       System.out.println("Hey, Keep " + Obj.display());
   }
}

Note: Please refer to the screenshot of the code to understand the indentation of the code.

Screenshot of the code:

Output:

Add a comment
Know the answer?
Add Answer to:
The following question is about java programming. Is it true or false that one can send...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT