Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Basic Question on Return values and expressions 2

Status
Not open for further replies.

Guy1978

MIS
Apr 21, 2005
7
BR
I'm learning Java on my own from a book, and there are times when things are not well explained. I'm trying to understand the concept of return values.
When you have an expression like:
Code:
int x = SomeFunction(15);

static int SomeFunction(int number) {
....
}
...then SomeFunction returns a value, which is stored in x. When I read about constructors, however, it is always mentioned that constructors have no return value. How is it, then, that expressions such as the following are possible?
Code:
Change c = new Change();
or even
Code:
return new Change();
If the constructor Change() doesn't return anything, if it has no return value, then how can it be used in expressions?
I know this is a newbie question, but the concept is confusing me. Thanks for any clarification.
 
In this case, "has no return value" doesn't mean it returns nothing but you cannot change what the constructor returns.

You can think on a constructor as a createObject method that returns an object of a specified class, but you cannot include a return statement to return, for example, an int.

Maybe this link can get it clearer:

Cheers,

Dian
 
return new Change();

is identically to

Change c = new Change ();
return c;

It's not the constructor itself returning something here, but a function which returns a new created Object.

Code:
int x = SomeFunction(15);

static int SomeFunction(int number) {
....
}
should be
Code:
int x = someFunction (15);

static int someFunction (int number) {
....
}
to make the difference to a constructor more obvious.
Code:
Change c = new Change ();
To create a new object Change and assign it to c is a more correct description than 'c is initialized with the object, returned from the constructor.
Avoid 'return' in a certification-answer for constructors, but if you use a graphical debugger, you see the cursor returning from the ctor.

It's a question of context. 'return' might be interpreted strictly or not so strictly.

seeking a job as java-programmer in Berlin:
 
Ok Diancecht, I think I got it.
Upon reading some sections of that book in the link you sent me, I was just confused about one other thing.
In chapter 4, in the section MEMBER INITIALIZATION, the author talks about how "each primitive data member of a class is guaranteed to get an initial value", and that when you use a constructor, "you aren’t precluding the automatic initialization, which happens before the constructor is entered" (section CONSTRUCTOR INITIALIZATION). Ie, if your constructor initializes a data member to 7, the data member first gets assigned an initial value (such as 0 for an int), and then gets assigned a 7.
So my question is this...what's the default, no-arg constructor for? If data members get initialized to '0', '0.0' or 'null' automatically, what is the purpose of a no-arg constructor, whose job it is to initialize values to '0', '0.0' or 'null'???
Thanks
 
The no-arg constructor is just a convenience, so if your own constructor doesn't do anything special, you may omit it.

Code:
class Foo
{
        public Foo ()
        {
               // the parent ctor is allways called first
               super ();
        }
}
and
Code:
class Foo
{
        public Foo ()
        {
               // parent ctor called implicit
        }
}
and
Code:
class Foo
{
}
is identical.


seeking a job as java-programmer in Berlin:
 
On a point of interest, every class requires at least one constructor. I see them as special methods which are responsible for setting the initial state of an object, and which implicitly return the newly created instance of the class. If you omit all constructors from a class, the compiler will silently include a default constructor for you. If you omit the default constructor, but include one with parameters, the compiler will leave it at that.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Ok, point by point.

1.- There are two different initialization stages when you create an object.

The first one is the, let's call phisycal stage where the member variables are initialized by the system. Primitive types (ints, chars) have a default value as they have some memory space that need to have any value (false for boolean, 0 for int). Objects are null by default, that means they still have no memort assigned.

The second one, let's call logical stage is where the constructor is executed and those member variables can be initialized according to the code in the constructor (int i=1; boolean init=true).

2.- What do you need a no arg constructor if you're just calling super inside? Well, just for nothing, it's automatically included. But it's useful if you want to initialize some variables always in your class (a boolean set to true, a Vector ready to store data ...)

Cheers,
Dian

 
Diancecht: ok, thanks for your explanation of the first point. But my question builds exactly on that. If the primitive member variables are automatically initialized at the physical stage, then why must a no-arg constructor be used at the point of object creation, if you don't specify and declare any other constructors?

From what I've learned, the no-arg constructor is used to do exactly what automatic initialization does at the physical stage (ie, set int and byte variables to 0, doubles and floats to 0.0, etc). Isn't that redundant? I understand the concept of having constructors that set other values to the member variables. But I don't understand why a no-arg constructor is provided (when you don't specify any constructors), if its only purpose is to do what automatic initialization has already done at the physical stage of object creation.

Thanks
 
Mainly because an Object is more than an accumulation of member variables. Each class you implement inherits Object, and for being an Object, it has some internal variables and data that must be initialized.

Let's say you have a car. You can state which colour is the car, which wheels you want, how many horsepowers or just order a car. But in all cases, the car has to be built.

Cheers,
Dian
 
You have the right to a constructor, if you cannot afford a constructor one will be created for you.

1.) Also, the object needs to be returned from somewhere. To create an object you need to call it's constructor. If you didn't write one, and one wasn't provided this wouldn't work:
Code:
Foo foo = new [red]Foo()[/red]
;
// Foo() is a call to a constructor, if it didn't exsist,
// there'd be no way for me to say "give me a foo object.
[/code]

Like wise, if you want to stop an object from being instanted you'd have to create a private constructor:
Code:
public class foo
{
  [green]/** Now we can't create a foo, and this won't be Javadoc-ed
   *
   **/[/green]
  private foo()
  {
  }
}

Also, automatic initializations happen at the construtors call, before the body of the constructor executes... This means that the automatic initialization can't be done if there is no call to a constructor, and that means that you need to have a constructor, even if it's the one created for you. Then there is what Dian was saying about building the base objects.

What do you need a no arg constructor if you're just calling super inside? Well, just for nothing, it's automatically included. But it's useful if you want to initialize some variables always in your class (a boolean set to true, a Vector ready to store data ...)
There is another very good reason to have it, even if all you is call super(), and that is javadoc. It is a good practice to get into to javadoc all functions, and the provided construtor should be javadoc-ed too. So, you'll sometimes see:
Code:
class foo
{
  [green]/** Constructs a new foo with the values ...
   *
   **/[/green]
   public foo()
   {
     super();
   }
}

[plug=shameless]
[/plug]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top