Quantcast

A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

thangeram

  • 2 years ago

look at the both programs Carefully

  • This Question is Closed
  1. thangeram
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    1- public class scannerClass { scannerClass (int a) // a constructor which print value a { int av; av=a; System.out.println(a); } public static void main(String ar[]) {int k; scannerClass a = new scannerClass(32); //value is pass directly when object is created //a.??? only Object class method will be shown }} 2- public class scannerClass { int ScannerClass(int a) //constructor with return type { int av; av=a; System.out.println(a); return a; } public static void main(String ar[]) {int k; //scannerClass a = new scannerClass(32);-error //here we cant pass value directly scannerClass a = new scannerClass(); a.ScannerClass(322); a.ScannerClass(32); } In first program we cant pass value by using object 'a' . where 2nd we cant pass value by directly when object is created why there is difference in between both programs ?

  2. eSpeX
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    The first thing I would ask is that you include the error you are given in the second class example. The information you give sounds as though it is not seeing your constructor and thus calling a default constructor. Also, you do not have any class member functions so once the instance is created you have no means of manipulating the variables. Try adding "set/get" functions instead of creating two instances of the class with the same name.

  3. rsmith6559
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    AFAIK, neither constructors or destructors can return values. They're just for initializing / cleaning up.

  4. eSpeX
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    @rsmith6559 is correct, constructors and destructors are part of the family of functions that do not return a value, as is the void function. Which does explain why your second program is calling a default constructor.

  5. thangeram
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    @eSpeX u mean in second program which have a constructor with return type it is not constructor at all and when we calling the object 'a' which are created is calling a default constructor ?

  6. eSpeX
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    @thangeram Yes, where you have "int ScannerClass(int a) //constructor with return type", this is an illegal form for a constructor and most likely the reason you are unable to issue the "a.ScannerClass(322);" command since the default constructor the compiler is using takes no parameters, ScannerClass(void).

  7. thangeram
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    ok @eSpeX then the program assum as another function unlike constructor and thats y we can call a.ScannerClass(322) thanx everyone :)

  8. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.