473,396 Members | 1,860 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Benefits using Properties?

I programmed JAVA for a while and starting C#, and was experimenting
with
"Properties". Question I have, is what is the benefit? JAVA does
not have the concept
of properties, so what is JAVA missing and why is it so great C# has
it? Thanks,

Yin

Sep 8 '07 #1
4 3113
The Java concept of setting and getting class instance and virtual method
values is what is more formally known as "properties". So, yes, you do have
properties in Java. This makes your main question mute. If you can
understand why a class should hold data that is related to the class
instance, then you understand why properties are an integral part of most
classes. Technically, properties aren't a C#, Java, or any language for
that matter, thing. They are a a basic tenant of Obejct Oriented
Programming.
"Yin99" <ws@ziowave.comwrote in message
news:11**********************@19g2000hsx.googlegro ups.com...
>I programmed JAVA for a while and starting C#, and was experimenting
with
"Properties". Question I have, is what is the benefit? JAVA does
not have the concept
of properties, so what is JAVA missing and why is it so great C# has
it? Thanks,

Yin

Sep 8 '07 #2
On Sat, 08 Sep 2007 11:47:46 -0700, Yin99 <ws@ziowave.comwrote:
>I programmed JAVA for a while and starting C#, and was experimenting
with
"Properties". Question I have, is what is the benefit? JAVA does
not have the concept
of properties, so what is JAVA missing and why is it so great C# has
it? Thanks,

Yin
C# properties are syntactic sugar - nice to have but not essential.

C#:

widget.Field = 45;
myVariable = widget.Field;

Java

widget.setField(42);
myVariable = widget.getField();

rossum

Sep 8 '07 #3
It is just syntactic sugar, but then again syntactic sugar is a big part of
any programming language.
I would phrase the question the opposite way: given that most other modern
programming languages (C++/CLI, VB, C#, Python, ...) have properties, why
doesn't Java? Not to mention other basic elements of modern languages:
delegates, events, operator overloading, a preprocessor, and 'ref' parameters
- some of this stuff sure comes in handy.
--
David Anton
http://www.tangiblesoftwaresolutions.com
Convert between VB, C#, C++, and Java
Instant C#
Instant VB
Instant C++
C++ to C# Converter
C++ to VB Converter
C++ to Java Converter
"Yin99" wrote:
I programmed JAVA for a while and starting C#, and was experimenting
with
"Properties". Question I have, is what is the benefit? JAVA does
not have the concept
of properties, so what is JAVA missing and why is it so great C# has
it? Thanks,

Yin

Sep 9 '07 #4
"Yin99" <ws@ziowave.comwrote in message
news:11**********************@19g2000hsx.googlegro ups.com...
>I programmed JAVA for a while and starting C#, and was experimenting
with
"Properties". Question I have, is what is the benefit? JAVA does
not have the concept
of properties, so what is JAVA missing and why is it so great C# has
it? Thanks,

Yin
As other have pointed out, properties are the C# equivalent of
setter/setter methods.
You could write C# code using the same getter/setter syntax as in Java
if you prefer, but properties improve the readability of the code. Below
are similar classes in Java and in C#.

------------------------------------
//// Java ////
public class Foo
{
private int x = 0;

public int getX() {return x;}
public void setX(int value) {x = value;}
}

//// C# ////

public class Foo
{
private int x = 0;

public int X
{
get {return x;}
set {x = value;}
}
}

-----------------[ setter example ] ----------------
// Java - You call the setter method explicitly
Foo foo = new Foo();
foo.setX(5);

// C# - X looks like a public field,
// but it really calls the setter under the covers
Foo foo = new Foo();
foo.X = 5;

-----------------[ getter example ] ----------------
// Java - You call the getter method explicitly
int x = foo.getX();

// C# - X looks like a public field,
// but it really calls the getter under the covers
int x = foo.X;

-----------------[ += example ] ----------------
// Java : += logic is a bit painful to look at

// add 2 to foo.x
foo.setX(foo.getX + 2));

// C# - Under the covers, this is doing the same thing as the java code
foo.X+=2;

-----------------
If you are interested in the underlying functionality try running this
code.
It explicitly shows how the setter/getter are called

using System;

public class Test
{
public static void Main()
{
Foo foo = new Foo();
foo.X = 5;
Console.WriteLine("Foo.X:{0}", foo.X);

foo.X += 2;
Console.WriteLine("Foo.X:{0}", foo.X);
}
}

public class Foo
{
private int x = 0;

public int X
{
get
{
Console.WriteLine("Foo.getX:{0}", x);
return x;
}
set
{
x = value;
Console.WriteLine("Foo.setX({0})", x);
}
}
}

Hope this helps
Bill




Sep 9 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: jova | last post by:
What are some of the benefits of using a hash table data structure?
0
by: Hamed | last post by:
Hello I have employed as a developer in a software company that its team uses FoxPro / VB 6.0 / VC++ 6.0 as the developing tools and newly is going to migrate to VS.NET. There is a project...
6
by: news | last post by:
I need some concise, easy to grok examples of what .Net (preferably C#.Net) has over just using old ASP or PHP with JavaScript. See, I'm a PHP guy, and while I started server-side scripting with...
13
by: Stijn Verrept | last post by:
Hi, I need to make a small web-application and was thinking about doing it in ASP.NET. Another developer (which is also network engineer) says it is hazardous to run .NET on his IIS server. He...
7
by: Norman Wooten | last post by:
Ok, I currently program in visual basic 5.0 (ouch, i know its old), Classic ASP & VBScript... I am about to make the jump to Visual Studio 2003, yet I have questions.. C#.NET I hear is quite...
6
by: denis | last post by:
What are the benefits of using const functions? How does the compiler interpret const functions? Thanks, Denis
4
by: arrastogi71 | last post by:
Kindly tell me the costs and benefits of using exceptions?
5
by: mclueless | last post by:
Can n e one tell me the benefits of using an MDI form??? In my project (vb6 n access) i have a main form from where the control goes to other forms depending upon what option the user selects. I...
1
by: Critical Skills, Inc. | last post by:
Oracle Advanced Benefits Analyst / Lead Start: 08/15/2004 End: 03/01/2005 Pay Rate: $80 - $85 per hour Job Duties: Must be open to travel. Must have designed and implemented...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.