Helpful Information
 
 
Category: Java
constructors

if you had something like this


class Blah
{

void Blah()
{
}

Blah()
{
}
}

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

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.

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?

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.

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?

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).

so like


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

that would have 2 instance variables?

Yes, height and distance.
public Blahhowever, is a syntax error. Since it's a class, you cannot omit the class keyword:
public class Blah {

OK thanks!

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; /
}
}

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.

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?

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;
}
}

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.










privacy (GDPR)