Board index » delphi » Properties in Java ?

Properties in Java ?


2007-10-12 10:33:35 AM
delphi186
Hi, I'd like to know if Java has properties like Delphi. I heard that it has something with the same effect but i couldn't find an example. Can you share any example or link?
Thanks in advance,
Lucas
 
 

Re:Properties in Java ?

Properties are implemented as "get" and "set" methods. It is a pattern
rather than an actual language feature in Java, unlike Delphi. In
Java, if you want a class with a Name "property" you have getName and
setName methods. Hope that helps.
Cheers,
Kevin.
Lucas writes:
Quote
Hi, I'd like to know if Java has properties like Delphi. I heard that it has something with the same effect but i couldn't find an example. Can you share any example or link?

Thanks in advance,

Lucas
 

Re:Properties in Java ?

"Lucas"
Quote
Hi, I'd like to know if Java has properties like Delphi. I heard that
it has something with the same effect but i couldn't find an example. Can
you share any example or link?

Thanks in advance,

Lucas
This question would be better asked in the JBuilder non tech or language
newsgroups. This is a Delphi forum.
However, since you are asking in comparison to Delphi, I will briefly
induldge you.
Properties, in Delphi are defined as attributes of a class that have a
Properties designation:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
// Class with Indexed properties
TMyClass = class
private
fName : String;
fAddress : String;
function GetName: String;
procedure SetName(name:String );
public
Property Name : read GetName write SetName;
In Java, unless class attributes are declared public, you can only write to
them in the class to which they belong (unless declared protected = from a
derivate class).
public class MyClass {
private String name;
private String address;
public String getName(){
return class;
}
public void setName(String name){
this.name=name;
}
}
To call property in Delphi, just call property
MyClass.name="Jay Jones";
To call in Java, do a set or get
MyClass myClass= new MyClass();
myClass.setName("Jay Jones");
myClass.getName();
To get the properties
Delphi
MyClass.name;
In Java:
myClass.getName();
If you delcare the attribute public in Java, then you can call it direct
public class MyClass {
public String name;
public String address;
public String getName(){
return class;
}
Call it
MyClass myClass= new MyClass();
//Set the value for MyClass.name
myClass.name="Jay Jones";
//Get the name
System.out.println(myClass.name);
Properties, in Java are part of a collection.
Properties pr- new Properties();
pr.load(new FileInputStream("someFile.properties"));
pr.get("someValue");
//or set a property
pr.setProperty("name","JayJones");
System.out.println(pr.get("name"));
Hope this helps!
 

Re:Properties in Java ?

danny heijl writes:
Quote
Paul Nichols [TeamB] schreef:

>However, since you are asking in comparison to Delphi, I will briefly
>induldge you.

To which I could add that a Java "bean" is nothing more than a class
featuring get and set methods to encapsulate fields (and a parameterless
constructor). These fields are than recognised as "properties" (using
reflection) by all kinds of software that instantiates these "beans".

Well there are two types of Java Beans, Visual Components and Java
Classes that are instantiated as Objects, not components. That does lead
to confusion some times.
Visual or Component Java Beans, do provide properties similar to Delphi
and BCB. These components use a property sheet to define their
attributes as properties. If that is the discussion, then we need to
follow a different pattern that the one I was following.
Java Beans that inherit from Component can be defined as having a
property sheet and can be made Visual (like a VCL class).
 

Re:Properties in Java ?

Lucas writes:
Quote
Hi, I'd like to know if Java has properties like Delphi. I heard that it has something with the same effect but i couldn't find an example. Can you share any example or link?

Thanks in advance,

Lucas
If you use Eclipse, you can refactor private properties. The getters and
setters will be declared for you. that is how I learned it.
I still prefer Delphi's and C#'s way of doing things.
M
 

Re:Properties in Java ?

Moni writes:
Quote
Lucas writes:
>Hi, I'd like to know if Java has properties like Delphi. I heard
>that it has something with the same effect but i couldn't find an
>example. Can you share any example or link?
>
>Thanks in advance,
>
>Lucas
If you use Eclipse, you can refactor private properties. The getters and
setters will be declared for you. that is how I learned it.

I still prefer Delphi's and C#'s way of doing things.

M
Nearly all Java IDEs have this functionality, including JBuilder.