PDA

View Full Version : constructors



craigtb
01-30-2007, 01:47 AM
if you had something like this


class Blah
{

void Blah()
{
}

Blah()
{
}
}

I know Blah() is a constructor, but the class Blah is not right?

Twey
01-30-2007, 01:25 PM
The second Blah() is a constructor, yes.

Of course the class isn't a constructor. A constructor is a method that sets up an instance of its class to be ready for use.

craigtb
01-30-2007, 02:14 PM
SO it has 2 constructors right?
I read somethign about what the void meant and didnt really understand it, can you try and explain it to me?
Also what does encapsulated mean?

Twey
01-30-2007, 04:02 PM
SO it has 2 constructors right?No, only one constructor. The other method has a return type, so it's just a standard instance method.
I read somethign about what the void meant and didnt really understand it, can you try and explain it to me?It's the return type declared when a method doesn't return a value.
Also what does encapsulated mean?
encapsulate

v 1: enclose in a capsule or other small containerI can't be any more specific without context.

craigtb
01-30-2007, 05:22 PM
Would this be tightly encapsulated?

so void Blah() is not a constructor since it does not return a value?

if im not mistaken an instance variable would be like String or int right?

Twey
01-30-2007, 06:22 PM
Would this be tightly encapsulated?"Tight encapsulation" refers to making the innards of the class as inaccessible to outside objects as possible. The idea is that this promotes the design of classes with consistent and powerful interfaces (I don't use the term in its Java sense) whose use requires no knowledge of the workings of the class.
so void Blah() is not a constructor since it does not return a value?No, it's not a constructor because it has a defined return type. The fact that that return type is void is neither here nor there.
if im not mistaken an instance variable would be like String or int right?An instance variable (known as a property in Java parlance) is any property that belongs to instances of the class, as opposed to, say, a local variable in a function or a static property (which belongs to the class itself).

craigtb
01-30-2007, 06:35 PM
so like


public Blah
{
private double height, distance;
void Blah()
{}
Blah()
{}
}

that would have 2 instance variables?

Twey
01-30-2007, 06:56 PM
Yes, height and distance.
public Blahhowever, is a syntax error. Since it's a class, you cannot omit the class keyword:
public class Blah {

craigtb
01-30-2007, 08:06 PM
OK thanks!

craigtb
01-30-2007, 08:37 PM
If i was to write a constructor for a class PersonInfo would it be somehting like this?


class PersonInfo
{
private String name, address;
private int income;
PersonInfo()
{
return name; \
return address; im not sure about this way of writing it
return income; /
}
}

Twey
01-30-2007, 08:54 PM
Er... no.

You don't return anything from a constructor. That's why it has no return type.

There's no need to even have a constructor if you're not going to do anything with it.

craigtb
01-30-2007, 09:12 PM
Im doing somethign that makes me need to write a constructor that includes the name of a person, their address and income. How would i do that?

Twey
01-30-2007, 10:43 PM
What do you mean it needs to "include" the name, address, and income of a person? You mean those details should be passed to it?
class Person {
String name, address;
float income;

Person(String name, String address, float income) {
this.income = income;
this.address = address;
this.name = name;
}
}

craigtb
01-30-2007, 11:12 PM
include meaning contain it. ( im really not sure). i think its saying that i need to have it in it. I think what you have might be what i need but i dont understand the this.income, this.address etc part.

Sorry to be so unclear about what i need. im not so sure myself.