#Fields vs Attributes

76 messages · Page 1 of 1 (latest)

thick python
#

Why do we have these two names? (fields and attributes) They seem to do the exact same thing.

Is there a subtle difference between the two?

For example a function and a method. I have some heard people say they are the exact same thing, but there is one key difference: a function is global, hence no functions in Java, only methods, since everything in encapsulated inside of a class.

Is there some difference like this to fields and attributes?

feral agateBOT
#

This post has been reserved for your question.

Hey @thick python! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

mental tinsel
#

“attributes” dont exist

#

it’s just fields

#

of which there are instance fields and static fields

boreal musk
thick python
mental tinsel
#

yes

thick python
#

color can be unique for each house

#

but streetstyle is shared, for all house objects, so it's static

mental tinsel
#

correct

thick python
#

and you cannot declare these elsewhere

#

they have to be in the className {}

#

you can create new instance based fields within each object

#

but you cannot change static fields, correct?

#

per each object

#

is there a word for this? "create new instance based field"? i don't think I am changing, I am making a new property for the object, based on the data type of the instance based field "String color" above

#

in other words, if you do this:
Neighborhood.streetStyle = "Classic";

#

for a static field

#

are you allowed to do that within the object itself?

#

or for static, there is only one, and that is always defined in ClassName{}, in this case Neighborhood class

#

so if there are no unique neighborhoods for each house object, since it is static, couldn't you just do this?
class Neighborhood { static String streetStyle = "Classic"; }

#

hmmm, so what's the point of doing it this way?

#

seems like extra work

gleaming field
thick python
#

And if you want it to be class based field, you have to declare the field as static

#

Why did Java decide on the wording “static”? It seems like an odd word to use

boreal musk
#

regarding your first two messages: yes

#

regarding the latter: There's also a keyword named static in C++ which does something a bit similar

mental tinsel
#

it has other uses in c++

boreal musk
#

something a bit similar

#

well at least in C it's somewhat similar

thick python
boreal musk
#

just somewhat similar

thick python
#

Is that why Java uses static?

boreal musk
#

regardng final - there is a difference between const and final

boreal musk
thick python
boreal musk
#

after all, they wanted to make Java easy for C++ developers

thick python
boreal musk
#

I think const (at least with structs in C) doesn't allow that

thick python
boreal musk
#

what do you mean with the same

#

once you change a.i and once you reassign a

thick python
#

you can do

final A a = new A();

but you cannot do

final A a = new A();
boreal musk
#

look at the second line

#

the second line in both pieces of code is what is different

#

the a=new A(); is what isn't possible

thick python
#

a=new A(); needs a class in front tho, doesn’t it?

#

A a = new A();

boreal musk
#

the variable already exists

#

it works the same for primitives

int i = 13;//create the variable

i = 37;//reassign the variable
thick python
#

I didn’t know this was possible for objects. Why would you want to make a new one with same name when the object has already been created?

boreal musk
#
A a = new A();
a=new A();

this would work

thick python
#

I don’t understand the reason for it

#

a has already been initialized

#

So why make a new object of same class with same variable name?

mental tinsel
thick python
#

Oh

#

Even if the object “a” was initialized as final

#

Couldn’t you reset each attribute for the object “a”?

boreal musk
#

or maybe you cannot change the state of the object because it doesn't allow you to do so (e.g. the fields are final)

#

or maybe the same object is used somewhere else