Java Forum / General / October 2006
properties
eotgibym@yahoo.com - 20 Oct 2006 20:09 GMT 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.
eotgibym@yahoo.com - 20 Oct 2006 20:22 GMT Well, I found a tutorial on properties and I think I will look at that first.
> 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 [quoted text clipped - 8 lines] > > how would I do this in java? Any help is appreciated. Bill Medland - 20 Oct 2006 20:24 GMT > 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 [quoted text clipped - 8 lines] > > how would I do this in java? Any help is appreciated. public int getMyValue() { return _myValue; } public void setMyValue(int value) { _myValue = value; }
 Signature Bill Medland
eotgibym@yahoo.com - 21 Oct 2006 16:13 GMT Thanks everyone.
> > 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 [quoted text clipped - 15 lines] > _myValue = value; > } Oliver Wong - 20 Oct 2006 20:57 GMT >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 [quoted text clipped - 8 lines] > > 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
Christoph Dahlen - 21 Oct 2006 08:41 GMT 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
 Signature Christoph Dahlen E-Mails nur an <vorname>@<nachname>.org
Simon Brooke - 20 Oct 2006 22:31 GMT > 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 [quoted text clipped - 6 lines] > 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; }
 Signature simon@jasmine.org.uk (Simon Brooke) http://www.jasmine.org.uk/~simon/
;; in faecibus sapiens rheum propagabit
Bill Medland - 21 Oct 2006 00:33 GMT >> 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 [quoted text clipped - 13 lines] > > 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.
 Signature Bill Medland
Mike Schilling - 21 Oct 2006 00:57 GMT >> 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 [quoted text clipped - 9 lines] > 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.
crazzybugger - 21 Oct 2006 01:26 GMT rightly said!
Jeff - 21 Oct 2006 02:47 GMT > 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.
crazzybugger - 21 Oct 2006 00:24 GMT > 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 [quoted text clipped - 8 lines] > > 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);
eotgibym@yahoo.com - 21 Oct 2006 16:37 GMT 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.
> 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 [quoted text clipped - 8 lines] > > how would I do this in java? Any help is appreciated. Jeff - 21 Oct 2006 16:56 GMT > 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 [quoted text clipped - 40 lines] > > > > how would I do this in java? Any help is appreciated. 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
eotgibym@yahoo.com - 21 Oct 2006 19:13 GMT 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.
> > 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 [quoted text clipped - 51 lines] > use MySQL for most of my database work. > /js eotgibym@yahoo.com - 21 Oct 2006 19:37 GMT 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.
> 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 [quoted text clipped - 87 lines] > > use MySQL for most of my database work. > > /js Oliver Wong - 23 Oct 2006 15:36 GMT > 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 [quoted text clipped - 25 lines] > 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
eotgibym@yahoo.com - 23 Oct 2006 17:15 GMT 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.
> 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
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|