properties

E

eotgibym

I am new to java (currently using C#). I am trying to learn java and I
am having a little difficulty in creating and using a property. Here
is a sample from C#:

private int _myValue;
public int myValue
{
get { return _myValue; }
set { _myValue = value; }
}


how would I do this in java? Any help is appreciated.
 
B

Bill Medland

I am new to java (currently using C#). I am trying to learn java and I
am having a little difficulty in creating and using a property. Here
is a sample from C#:

private int _myValue;
public int myValue
{
get { return _myValue; }
set { _myValue = value; }
}


how would I do this in java? Any help is appreciated.

public int getMyValue() {
return _myValue;
}
public void setMyValue(int value) {
_myValue = value;
}
 
O

Oliver Wong

I am new to java (currently using C#). I am trying to learn java and I
am having a little difficulty in creating and using a property. Here
is a sample from C#:

private int _myValue;
public int myValue
{
get { return _myValue; }
set { _myValue = value; }
}


how would I do this in java? Any help is appreciated.

There's no explicit language support for properties. They're implemented
as plain old methods in Java, usually named getMyValue() and
setMyValue(int).

- Oliver
 
S

Simon Brooke

in message <[email protected]>,
I am new to java (currently using C#). I am trying to learn java and I
am having a little difficulty in creating and using a property. Here
is a sample from C#:

private int _myValue;
public int myValue
{
get { return _myValue; }
set { _myValue = value; }
}

What's the benefit of making an instance variable private if you're going
to provide both a public getter and a public setter?

The way you'd to that in Java is

public int myValue = INITIALVALUE;

However if you wanted a value which could be publicly read but could not be
publicly written you'd use

private int myValue = INITIALVALUE;

public int getMyValue()
{
return myValue;
}

and if you wanted one that could be publicly written but not publicly read,
you'd have

private int myOtherValue = INITIALVALUE;

public void setMyOtherValue( int i)
{
myOtherValue = i;
}
 
C

crazzybugger

I am new to java (currently using C#). I am trying to learn java and I
am having a little difficulty in creating and using a property. Here
is a sample from C#:

private int _myValue;
public int myValue
{
get { return _myValue; }
set { _myValue = value; }
}


how would I do this in java? Any help is appreciated.

You donot have any properties kind of stuff in java. You have to use
normal getters and setters . It goes something like this
private int value;
public int getMyValue();
public void setMyValue(int val);
 
B

Bill Medland

Simon said:
in message <[email protected]>,


What's the benefit of making an instance variable private if you're going
to provide both a public getter and a public setter?

The way you'd to that in Java is

public int myValue = INITIALVALUE;

If you want to do this, Simon, then fine. However I would strongly advise
the OP against it.
By using a get and set method the programmer reserves the right to modify
the implementation at a later time without modifying the (api, interface,
appearance, whatever you want to call it).
It's the same reason for using a property in C# rather than a public
variable.
 
M

Mike Schilling

Simon Brooke said:
in message <[email protected]>,


What's the benefit of making an instance variable private if you're going
to provide both a public getter and a public setter?

It allows you to change the implementation later without breaking calling
code. It also allows you, during debugging, to trap or add tracing to any
calls to the accessors. And, in Java, it follow the pattern for JavaBeans,
which allows your code to be used with many sorts of frameworks.
 
J

Jeff

crazzybugger said:
rightly said!

Using getters and setters also allows the object to handle validation.
Therefore, if you share your class/object, you can catch that a
different programmer tried to use a negative number for a port value,
for example.

About the only time it is proper to have a property directly exposed,
IMHO, is if it is to be overridden. For example, some libraries have a
PrintProperties class that is used to set the print properties used to
print some other class. As a programmer you are expected to create a
class that extends that class, override the defaults you wish, and then
use your class as an argument fed to the print engine for the library.
In that case, having a property called Orientation which can have
values of Landscape or Portrait and which is simply set in the class
definition as a public property is reasonable.
 
C

Christoph Dahlen

Am Fri, 20 Oct 2006 19:57:09 +0000 schrieb Oliver Wong:
There's no explicit language support for properties. They're
implemented
as plain old methods in Java, usually named getMyValue() and
setMyValue(int).

By the way, the naming convention above is not only a recommendation, is
it infact required for various tools (IDE etc.) and frameworks (JSP,
JSF) to work.

And nobody mentioned to far, that boolean properties' getter has to start
with 'is'.

private String name;
private boolean valid;

public void setName(String name) { this.name = name; }
public String getName() { return this.name; }
public void setValid(boolean valid) { this.valid = valid; }
public boolean isValid() { return this.valid; }

Christoph
 
E

eotgibym

OK, I am back. I completely under the getter/setter concept. However,
what I am trying to do is create a property that can be used to pass
values between forms (JFrames).

If I had two JFrames, one being say, employee basic information
(Form1), and the other containing extended info about the same employee
(Form2). When I open Form1 to an employee record, then, in Visual
Studio for example, I would then create a property in Form2 like so:

private string _employeeID;
public int employeeID
{
get { return _employeeID; }
set { _employeeID= value; }
}

In Form1 I set the Form2 property when the form instance is created as
follows:

Form2 frm = new Form2();
frm.employeeID = xxxxx

Finally, in Form2, I can use the _employeeID value to populate the form
with data.

Most of my apps I do are based off of a database design or concept, and
a significant part of the model for my applications start there (at the
database). Anyone know of any good tutorials or books regarding
database-cebtric application design using Java?

Thanks again.
 
J

Jeff

OK, I am back. I completely under the getter/setter concept. However,
what I am trying to do is create a property that can be used to pass
values between forms (JFrames).

If I had two JFrames, one being say, employee basic information
(Form1), and the other containing extended info about the same employee
(Form2). When I open Form1 to an employee record, then, in Visual
Studio for example, I would then create a property in Form2 like so:

private string _employeeID;
public int employeeID
{
get { return _employeeID; }
set { _employeeID= value; }
}

In Form1 I set the Form2 property when the form instance is created as
follows:

Form2 frm = new Form2();
frm.employeeID = xxxxx

Finally, in Form2, I can use the _employeeID value to populate the form
with data.

Most of my apps I do are based off of a database design or concept, and
a significant part of the model for my applications start there (at the
database). Anyone know of any good tutorials or books regarding
database-cebtric application design using Java?

Thanks again.

Presuming you are selecting the employee in Form1, you can expose a
method in Form2 that is called by Form1 when the employee ID is
changed. That method would then change the displayed data on Form2 to
keep the info in sync. So, rather than have Form2 pull the info from
Form1 as a property, you push the info from Form1 to Form2 through an
exposed method.

I like MYSQL and Java Developer's Guide by Mark Mathews, et al, but I
use MySQL for most of my database work.
/js
 
E

eotgibym

Hi Jeff, Thanks for the info. Thats what I want to do. The employeeID
is pulled from form one. the property is in form2, and form1 pushes
the value to the property in form2 (again C#):

Form2 frm = new Form2() // create a new instance of form2
frm.employeeID = this.txtEmployeeID.Text; // set form2's employeeID
property
frm.ShowDialog(); // open the form

the syntax above is what I am looking for, and the data is loaded in
form1. Based on what I have read here, I am thinking my code would
look like so, a combination of a property and a method:

private String _employeeID;

public void setEmployeeID(String employeeID)
{
_employeeID = employeeID;
}

I am not sure I would need a get statement as the private variable
_employeeID would only be used within the
current class. Then again, if wanted to access the employeeID from
outside the class, would I add the following?:

public int getEmployeeID()
{
return _employeeID ;
}

Does this sound right? I realize this is pretty basic, but I think if
I understand this I would have a pretty good grasp. I guess I could
try it.

Thanks again.
 
E

eotgibym

ok, I DID IT! I am a little stoked. here is what I did:

in form1 I opened the form2 and set the _employeeID property using a
method in form2:

Form2 frm = new Form2();
frm.setEmployeeID("1000");
frm.setVisible(true);

this is the method and property and method in form2:

private String _employeeID;
public void setEmployeeID(String employeeID)
{
_employeeID = employeeID;
}

very cool. thanks to everyone for pointing me in the right direction.
now I can really play.
 
O

Oliver Wong

OK, I am back. I completely under the getter/setter concept. However,
what I am trying to do is create a property that can be used to pass
values between forms (JFrames).

If I had two JFrames, one being say, employee basic information
(Form1), and the other containing extended info about the same employee
(Form2). When I open Form1 to an employee record, then, in Visual
Studio for example, I would then create a property in Form2 like so:

private string _employeeID;
public int employeeID
{
get { return _employeeID; }
set { _employeeID= value; }
}

In Form1 I set the Form2 property when the form instance is created as
follows:

Form2 frm = new Form2();
frm.employeeID = xxxxx

Finally, in Form2, I can use the _employeeID value to populate the form
with data.

Most of my apps I do are based off of a database design or concept, and
a significant part of the model for my applications start there (at the
database). Anyone know of any good tutorials or books regarding
database-cebtric application design using Java?

Out of curiosity, why not create a seperate Employee object, and in
Form1, populate the Employee object, and when you want to display Form2,
just pass the Employee object over, without bothering to re-query the DB?

- Oliver
 
E

eotgibym

Good suggestion. For right now I am just basically trying to learn
some rudimentary things about the language. I am not actually writing
a production application, just trying to recreate functionality I
frequently use in visual studio to get familiar with the language. One
thing good about C# is its close similarity with java.

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top