• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Properties in Java

 
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Ranchers,
Need a Help!!!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic