Jump to content
  • Home
  • Files
  • Docs
Topics
  • All Content

  • This Topic
  • This Forum

  • Advanced Search
  • Existing user? Sign In  

    Sign In



    • Not recommended on shared computers


    • Forgot your password?

  • Sign Up
  • All Activity
  • Home
  • Non-Forge
  • Off-topic
  • Fields vs. Properties
Currently Supported: 1.16.X (Latest) and 1.15.X (LTS)
Sign in to follow this  
Followers 0
Draco18s

Fields vs. Properties

By Draco18s, March 6, 2018 in Off-topic

  • Reply to this topic
  • Start new topic

Recommended Posts

Draco18s    2406

Draco18s

Draco18s    2406

  • Reality Controller
  • Draco18s
  • Members
  • 2406
  • 15940 posts
Posted March 6, 2018

This is mostly a question directed at @diesieben07. One of these days I'll figure out why a Field and a Property aren't the same thing. I certainly recognize the difference between these as a declaration:

public float value1;
public float value2 { get; set; }

The problem is that from outside the class (i.e. seeing only someObject.value1 and someObject.value2, both of which can be read and written), they behave / appear to behave the same way (ignoring the fact that value2 can be made read-only or write-only).

 

In trying to look up the difference, I got these three descriptions (thanks Stack Overflow):

 

Quote
  • field

    • A data member of a class. Unless specified otherwise, a field is not static.
  • property
    • Characteristics of an object that users can set.

(Eesh, how vague.)

Quote

Field is generally a private variable on an instance class. It does not mean there is a getter and a setter.

Property is the getter and setter combination.

Quote

Fields must be declared and initialized before they are used. Mostly for class internal use.

Properties can be changed by setter and they are exposed by getters.

 

Which...doesn't help (underscoring added for emphasis). The only seeming distinction is the fields are (usually?) protected* and properties are (always?) public.

Wait, if fields are protected, why mention that "unless specified, they aren't static"? Is a static field still protected!?

 

*protected here meaning to refer to either protected or private.

  • Quote

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Share this post


Link to post
Share on other sites

diesieben07    7613

diesieben07

diesieben07    7613

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7613
  • 55180 posts
Posted March 6, 2018

I am not sure if there is a perfectly clear definition. However here is how I see it.

 

A property is a data-storing member of a class (when static) or an object (when non-static). There are several types of properties:

  • "POJO-Style": private field plus getter/setter.
    • The field should not be accessed directly (even inside the same class), it is only used as underlying storage and encapsulated via getter and setter, which can add things like validation or lazy initialization.
    • Setter can be left out to make the property read-only.
    • Getter and setter can be given different visibilities to e.g. restrict writes to the class itself.
  • Virtual property: Just a getter/setter
    • Value is computed on the fly whenever getter is accessed, possibly as a combination of other properties.
    • Same rules for getter and setter apply as above.
  • Just a field
    • Usually not a good idea, since you cannot change semantics of getting and setting the property without breaking binary compability (you have to introduce getter and/or setter)
    • can be made read-only by making it final.

As you can see, property and field are orthogonal concepts. A field is a means to implement a property.

  • Quote

Share this post


Link to post
Share on other sites

Draco18s    2406

Draco18s

Draco18s    2406

  • Reality Controller
  • Draco18s
  • Members
  • 2406
  • 15940 posts
Posted March 6, 2018
4 minutes ago, diesieben07 said:

A field is a means to implement a property.

THAT is a definition I can work with.

 

I'll probably still refer to the value in object.value as a field or property interchangeably, though, as it comes down to an implementation detail inside the class that isn't relevant when talking about "that thing that is named 'value' and is part of the definition of the thing named 'object' "

 

9 minutes ago, diesieben07 said:

Usually not a good idea, since you cannot change semantics of getting and setting the property without breaking binary compability

How often does this come up when not dealing with ASM or [thing I don't have a word for, that is what Forge does, patching? (de)obfuscation?]?

It's a question that I cannot even attempt to analyze because the only Java work I've ever done has been for Minecraft via Forge.

That is, outside of Forge and Forge-like software, how often is binary compatibility an issue?

  • Quote

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Share this post


Link to post
Share on other sites

diesieben07    7613

diesieben07

diesieben07    7613

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7613
  • 55180 posts
Posted March 6, 2018
1 minute ago, Draco18s said:

That is, outside of Forge and Forge-like software, how often is binary compatibility an issue?

All the time. Java projects often have a dependency tree the size of Texas (this is the dependency list of a current Spring Boot project I am working on, and I have barely started). If something updates and breaks binary compatibility, multiple types of excrement start hitting multiple types of propeller-based devices.

 

This is so much of a problem and nuisance, that Kotlin adds special syntax for properties.

  • Quote

Share this post


Link to post
Share on other sites

Draco18s    2406

Draco18s

Draco18s    2406

  • Reality Controller
  • Draco18s
  • Members
  • 2406
  • 15940 posts
Posted March 6, 2018 (edited)
10 minutes ago, diesieben07 said:

All the time. Java projects often have a dependency tree the size of Texas (this is the dependency list of a current Spring Boot project I am working on, and I have barely started).

/me adds it to the "list of things to stay away from," keeping NPM company.

 

Also

Quote

If something updates and breaks binary compatibility, multiple types of excrement start hitting multiple types of propeller-based devices.

http://www.stilldrinking.org/programming-sucks

Edited March 6, 2018 by Draco18s
  • Quote

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Share this post


Link to post
Share on other sites

diesieben07    7613

diesieben07

diesieben07    7613

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7613
  • 55180 posts
Posted March 6, 2018
3 minutes ago, Draco18s said:

/me adds it to the "list of things to stay away from," keeping NPM company.

The difference is that in the Java world this stuff usually works. NPM falls flat on it's face every other day.

 

4 minutes ago, Draco18s said:

http://www.stilldrinking.org/programming-sucks

Yup. Old but gold.

  • Quote

Share this post


Link to post
Share on other sites

Draco18s    2406

Draco18s

Draco18s    2406

  • Reality Controller
  • Draco18s
  • Members
  • 2406
  • 15940 posts
Posted March 6, 2018
23 minutes ago, diesieben07 said:

The difference is that in the Java world this stuff usually works. NPM falls flat on it's face every other day.

More like "Eh, I'm already not doing professional Java work, I'm now more incentivized not to." I'm sure it works Fine, but it's not "what I do" so I don't see a reason to make it "what I do."

I like modding Minecraft because there's a million billion things I don't have to worry about and I can make cool stuff.

  • Quote

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

    • Insert image from URL
×
  • Desktop
  • Tablet
  • Phone
Sign in to follow this  
Followers 0
Go To Topic Listing



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • fckurshitsite
      "shaderpacks" folder does not appear in Minecraft folder!

      By fckurshitsite · Posted 1 minute ago

      so rude and useless :^)
    • Silivek
      Mod won't load when in dev environment

      By Silivek · Posted 19 minutes ago

      I ran it using the runClient configuration generated by  gradlew genEclipseRuns in the command line.
    • kiou.23
      Mod won't load when in dev environment

      By kiou.23 · Posted 23 minutes ago

      That's odd, I couldn't find anything wrong with the code. could you describe exactly what steps you are taking to run the mod?   also: your group in build.gradle and your main path doesn't follow the convention, seeing that you have a github, you could use that http://maven.apache.org/guides/mini/guide-naming-conventions.html   EDIT: Ideally I would pull the repo to my machine and try running the mod myself, but my weak computer takes 4 hours to decompile mc, So... sorry I can't do that
    • cadbane86140
      The Dream Skin Invasion!

      By cadbane86140 · Posted 24 minutes ago

      Hello There! This is a very weird but something worth documenting video on my channel, so late last night I see the usual stuff trending on twitter but decide to go into the server and well this happened... Our reactions are hilarious and I know you guys are gonna love it! I hope you all enjoy this video and if you did don't forget to like and sub for more!  
    • kiou.23
      Crafting Damage (1.16)

      By kiou.23 · Posted 28 minutes ago

      you don't need the existing damage, you can call ItemStack#damage() on the stack
  • Topics

    • DenizAri
      6
      "shaderpacks" folder does not appear in Minecraft folder!

      By DenizAri
      Started January 24, 2020

    • Silivek
      6
      Mod won't load when in dev environment

      By Silivek
      Started Thursday at 05:46 PM

    • cadbane86140
      0
      The Dream Skin Invasion!

      By cadbane86140
      Started 24 minutes ago

    • DeadZone
      4
      Crafting Damage (1.16)

      By DeadZone
      Started 2 hours ago

    • GermanBucket
      1
      IntelliJ doesn't include resources folder while debugging

      By GermanBucket
      Started 40 minutes ago

  • Who's Online (See full list)

    • Initifi
    • Draco18s
    • fckurshitsite
    • Silivek
    • Godis_apan
    • LUCy2021
    • LeiteDesnatado
    • Extrodonary
    • HowBoutNo
    • kiou.23
  • All Activity
  • Home
  • Non-Forge
  • Off-topic
  • Fields vs. Properties
  • Theme

Copyright © 2019 ForgeDevelopment LLC · Ads by Longitude Ads LLC Powered by Invision Community