File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Properties in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Properties in Java" Watch "Properties in Java" New topic
Author

Properties in Java

Shrinivas Mujumdar
Ranch Hand

Joined: Aug 27, 2004
Posts: 328
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

Joined: Sep 16, 2005
Posts: 1780
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/


There is no emoticon for what I am feeling!
Shrinivas Mujumdar
Ranch Hand

Joined: Aug 27, 2004
Posts: 328
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

Joined: Jan 10, 2002
Posts: 60059
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13881
    
  10

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
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

Joined: Aug 27, 2004
Posts: 328
Hello Jesper,
Thanks for Reply first of all

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


Shrinivas
Shrinivas Mujumdar
Ranch Hand

Joined: Aug 27, 2004
Posts: 328
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

Joined: Jan 17, 2006
Posts: 1296
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 ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13881
    
  10

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

Joined: Sep 16, 2005
Posts: 1780
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

Joined: Aug 27, 2004
Posts: 328
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

Joined: Aug 27, 2004
Posts: 328
Hey Ranchers,
Need a Help!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Properties in Java
 
Similar Threads
processing with arrays
Java's use in Microsoft
What about the future of j2ee
Accessing constants by variable name
Junit problem, how to load a properties file