• Post Reply Bookmark Topic Watch Topic
  • New Topic

Return statement in a constructor  RSS feed

 
Suvarchala Malgudi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone let me know the difference between two statements:

Class1 class1 = new Class();
class1 = Class2.method1();

and

Class1 class1 = Class2.method1();


I have one more query on the same lines ... I always need to call the method1 of Class2 whenever i create a object of class1. So I wanted to go with the constructor in Class1. But the method1 in Class2 has a return statement. so is there any better way to do this other than constructors.

Sample code:





 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ambareesh Balisetty wrote:Can anyone let me know the difference between two statements:

Class1 class1 = new Class();
class1 = Class2.method1();

and

Class1 class1 = Class2.method1();


The first one creates a new Class1 object on the first line and then uses then discards that object by pointing its reference class1 to another object. you should not create objects you are not going to use so you should use the firststatements in your code.

Ambareesh Balisetty wrote:
I have one more query on the same lines ... I always need to call the method1 of Class2 whenever i create a object of class1. So I wanted to go with the constructor in Class1. But the method1 in Class2 has a return statement. so is there any better way to do this other than constructors.

What are these classes representing in the real world and what do the methods do? Generally you should use a constructor to initialize state only but without knowing what the methods are doing and what the classes represent it's difficult to say.
 
Suvarchala Malgudi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator






At present, to get the DetailsReader, i'm using following code:


But instead i want to write only this statement
DetailsReader detailsReader = new DetailsReader();

When i call this statement, it has to call the constructor in DetailsReader class. so i wanted to create a constructor in DetailsReader. Byt i'm baffled since we cannot return using a constructor.
Is there any bettter thing to do other than the constructor?
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not following. Why do you need to call the constructor? Also, in the call to the method returns a DetailsReader object right? So why doesn't that method call the constructor when it creates that object before returning it?
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
Always use code tags; I have added them and you can see how much better the code looks

I agree with E Armitage that your question is difficult to understand. You posted two code snippets, neither of which would compile, in one case because of a syntax error, in the other because Class() hasn't got any public constructors; maybe you wanted to write new Class1(). Then you seem to be confusing constructors and factory methods. Have you created a class Class with the same name as a class in the java.lang package?
Why are you passing a Class<?> object to a method or a constructor? That is looks like reflection, which is usually a bad way to run a Java program.
I suggest you start from scratch. Tell us simply what you are trying to do. do you actually know how to write a constructor?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!