Why wont this shitty Java code work? Why can't I access the property of a class?

Why wont this shitty Java code work? Why can't I access the property of a class?

Not sure if bait.

post the rest of your code.

No (You)s for you.

Its an honest question. Why the fuck wont it let me access the property if I point an Object to the fucking shit


The hashmap contains objects of a class which has a protected property called name hence hashMap.get(1).name is valid.

Show the declaration / initialization of the Map or gtfo

java sux

use bash instead

Actually, you just aren't casting it to your class type correctly

Map hashMap = new HashMap();

hashMap.put(1, new LeClass("faget"));

class LeClass{

protected String name;
}

Of course the LeClass contains a constructor that sets the name as well but i forgot to include it in my post

What?

yep you're definitely not casting right.

Object can't access the attributes, so you have to declare the 'obj' variable as LeClass, otherwise it can't see the fields.

In the OP you are retrieving a value from from your map but assigning it to a variable of type Object. Object does not have a name property, there is no way of knowing at compile time that name exists, so that's why you're getting an error.

Why don't you just declare obj as type LeClass instead of Object?

Ah i see, thanks

example.

also don't use protected fields and access directly, use private fields and getters/setters. eclipse even has boilerplate generators for it.

surely i'm not the only one who dislikes using getters and setters for every field, especially when there's no get/set logic to be done nor will there ever be. basic record-type style objects should just expose, imo

you might not like it, but it's good practice for java.

Yeah, i dont use getters and setters for primitive types

thing.setValue(thing.getValue() + other.getValue());
vs.
thing.value += other.getValue;
vs.
defining yet another method for changing a field...

There are some cases where it's alright to make a public variable and access it directly, and then there are other cases where you're trying to figure out why your variable is fucked up and you spend hours digging though decade old code to find that someone accessed it directly and added some random ass value to it to bypass some bug that they were too lazy to properly fix.

Correct me if I'm wrong, shouldn't you check if the key exists in the first place? (this is a runtime error, irrelevant to the compile-time one as in OP's pic related)

E.g. In C#
if(myDic.hasKey("shitStain")) myDic["shitStain"].shittyDateField = DateTime.Now;

>phone CS shitposting

>shouldn't you check if the key exists in the first place
Why? I already know it exists ;^)

The less code the better

Enjoy your NullPointerExceptions

Also, dude/t there is no name field in Object, you have to downcast to your type which is the type of Value in Key-Value pair in your "god-tier" hash map. Subtypes expose supertypes but not vice-versa. Gotta down cast bruh.

Ma man.


>all-original Syrian CS shitposting continues