File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Properties in Java

 
Shrinivas Mujumdar
Ranch Hand
Posts: 328
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,
I was curious to know how the properties works in Java?. Just to elaborate my point:
Let's consider that i have a Array named as "arr" now if i want to print each element in this array, i will use for loop where i would say something like , now internally how this return length, I think similarly i can use .class property to load class in memory. How this works?, How many such properties are existing ? and Can we write our own custom properties (Its avaialble in C#.net)
I referred to many docs on net but could not get the answer...

Thanks in Advance,
Shrinivas
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The term "property" isn't in common usage in Java, at least I don't use the term in any technical context... I think what you are alluding to are just public fields, for example:

If you want to find out the public fields of a class, use the API: http://java.sun.com/j2se/1.5.0/docs/api/
 
Shrinivas Mujumdar
Ranch Hand
Posts: 328
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Jeff,
I am clear with the explanation you have provided , thanks for that but not very much clear how you will apply this logic to Arrays (of primitive Data Types & any Object ) as well as <<SomeClassName>>.class
where this class & length property is ?

Thanks once again
Shrinivas
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64181
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeff Albertson:
The term "property" isn't in common usage in Java


I'll disagree with that. The term property is used liberally within the java.util package with regards to resources and properties files, and it used equally liberally to describe the items exported by classes that follow the JavaBean convention.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The ".class" for any object and ".length" for arrays are special Java language constructions. The syntax looks the same as for accessing public member variables, but they are not really public member variables.

How Java handles these behind the scenes depends on the implementation of the compiler and the JVM you use, and you don't need to know or worry about how they do it.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jesper de Jong:
The ".class" for any object and ".length" for arrays are special Java language constructions. The syntax looks the same as for accessing public member variables, but they are not really public member variables.

How Java handles these behind the scenes depends on the implementation of the compiler and the JVM you use, and you don't need to know or worry about how they do it.


I always thought of length as a public final field. Is that incorrect?
 
Shrinivas Mujumdar
Ranch Hand
Posts: 328
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Jesper,
Thanks for Reply first of all

What about .class ?
.class is available for any class & Interface...


Shrinivas
 
Shrinivas Mujumdar
Ranch Hand
Posts: 328
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me rephrase the question..

Lets say there is a hierarchy..Employee<---Manager<---SalesManager & there is some other class say Test(Nothing to do with Employee Hierarchy)..right?

so i want one property (Userdefined..say ".base")
So that if i use this property for all the Four classes (& any other class i write later on )then it should display me their individual Super class name

See the code

System.out.println(Employee.base); //should print Object
System.out.println(Manager.base); //should print Employee
System.out.println(Manager.base); //should print Employee
System.out.println(Test.base); //should print Object

You could say this could be done through other mechanism..

Is this not similar to .length for array (of primitive or any object) & .class (for any class & interface)..a generic one...now there will be some code executed by JVM to find out the length of array & to load a specific class & Interface in memory.................question is How many such properties are existing & can i write my own (as in above example)?


Shrinivas
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would have to define the field within each class. There is no way to make it automatically *be* there.


Alternately you could use the class literal expression

[ April 20, 2006: Message edited by: Garrett Rowe ]
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeff Albertson:


I always thought of length as a public final field. Is that incorrect?


That's how it behaves in a Java progam, but it is not necessarily implemented behind the scenes in exactly the same way as a public final field.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jesper de Jong:


That's how it behaves in a Java progam, but it is not necessarily implemented behind the scenes in exactly the same way as a public final field.


Son-of-a-gun, you're right:
 
Shrinivas Mujumdar
Ranch Hand
Posts: 328
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I really appreciate your answers & satisfied to certain extent. Thanks a lot to Everybody, However, I didn't understand that how this .class & .length gets attached to every Class/Interface & Array anybody writes in Java?

Shrinivas
 
Shrinivas Mujumdar
Ranch Hand
Posts: 328
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Ranchers,
Need a Help!!!
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic