Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7719675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:28:31+00:00 2026-06-01T03:28:31+00:00

Java claims to be object oriented and typesafe, and Scala even more so. Internal

  • 0

Java claims to be object oriented and typesafe, and Scala even more so.

Internal Class fields are represented by class called Field, which you can obtain a reference to via the Reflection API.

My question: do these languages provide any way to obtain that Field reference in a typesafe way? (And if not, why on earth not? Seems like a glaring deficiency)

It would be extremely useful when mapping an Object to some external representation, for example to html fields in a template, or to column names in a database, to keep the reference names automatically in sync.

Ideally I’d like to say something like:

&(SomeClass.someField).name() 

to get the name of the field declaration, similar to how java enums let you say:

MyEnum.SOME_INSTANCE.name()

[update:] after reading feedback that this functionality would somehow violate the intent of the Reflection API, I agree that Reflection is designed for things that aren’t known at compile time, and that’s exactly why it’s so absurd to have to use it to learn things that are known at compile time, namely the Fields of the very class that it’s compiling!

The compiler provides this for enums, so if the compiler is able to access the enum Field’s reference to allow MyEnum.SOME_INSTANCE.name(), then there’s no logical reason why it shouldn’t also be able to provide this same functionality to ordinary Classes.

Is there any technological reason why this functionality couldn’t be there for ordinary classes? I don’t see why not, and I disagree that this functionality would “complicate” things… on the contrary it would vastly simplify the present cumbersome Reflection API techniques. Why force developers into Reflection to find out something that is known at compile time?

[update #2] as for the utility of this feature, have you ever tried using the Criteria API in JPA or Hibernate to dynamically construct a query? Have you seen the absurd work-arounds people have come up with to try to avoid having to pass in an unsafe String representation of the field to query against?

[update #3] Finally, a new JVM language called Ceylon has heeded the call and makes this trivial to do!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T03:28:32+00:00Added an answer on June 1, 2026 at 3:28 am

    My question: do these languages provide any way to obtain that Field reference in a typesafe way?

    Compile-time typesafe? Not that I’m aware of, at least in Java. The normal purpose of reflection in Java is for code to be able to deal with types it has no knowledge of before-hand – it’s rare (in my experience) to be in a position where you want to be able to refer to a field in a known type. It does happen, but it’s not very common.

    (And if not, why on earth not? Seems like a glaring deficiency)

    Every feature needs to be designed, implemented, tested, and has to meet the balance of providing more value than the added complexity in the language.

    Personally I can think of features I’d much rather see in Java than this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It's the first time I use java Rmi*. I have a custom class which
I always come across articles which claim that Java is interpreted. I know that
Do you agree that the designers of Java class java.io.IOException should have made it
I have the following Java annotation on a class (it's for a myBatis plugin):
The code at Java Tutorials showed an example of using DataOutputStream class and DataInputStream
I'm trying to map an @Embeddable object in a subclass whose parent class already
I've seen some interesting claims on SO re Java hashmaps and their O(1) lookup
In Java, the containsAll and retainAll in the AbstractCollection class explicitly state that cardinality
I am writing a Java web service for legacy SCO machines (which only have
I just stumbled upon MainSoft's Grasshopper , which claims to cross-compile .Net ILM to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.