#are objects' methods on the heap, or are they allocated in a special kind of memory?

467 messages · Page 1 of 1 (latest)

flat cypress
#

I wanna be sure if methods are on the heap or not. Also, I wanna be sure if an object's data is always going to be the variables

short glenBOT
#

This post has been reserved for your question.

Hey @flat cypress! Please use /close or the Close Post button above when you're finished. 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.

trail nimbus
#

for the second question; where else would they be?

flat cypress
#

at first i thought the object's data was the whole class it was based on

#

including the methods

#

but methods are allocated differently, right?

trail nimbus
#

yeah

#

"object's data" is something that each object has, but all objects of the same type share the same class things
everything in the class is shared between objects. the things that are unique to the objects are the values that the instance variables/fields/attributes/properties hold

#

constructors, methods, init blocks, all the static things, are shared

flat cypress
#

i see

#

there is a name to the kind of memory methods use, right?

trail nimbus
#

the variable declarations are shared, that part is in/of the class
but the variable values are not shared, that's what the data is

trail nimbus
flat cypress
#

phew that's a relief

#

i thought methods were part of the objects data lol

#

but it's stated that an object is on the heap so...

#

it wouldn't make any sense

#

unless it's possible for memory to contain other memory allocations

#

then that'd be... weird

#

dynamic memory holding stack memory

#

hm

#

i wonder if that's... possible

#

for an object

trail nimbus
#

values are nouns, methods are verbs
suppose you have a bank account
the data would be "owner", "value", "cards", "loans", that sort of thing right?
"withdraw", "deposit", "transfer" aren't data of the account

flat cypress
#

oh now you're speaking my language

#

syntax tree

#

the only problem being the GODDAMN SEMANTICS AND PRAGMATICS

#

don't even get me started on phonology

#

thanks for the help

short glenBOT
# flat cypress thanks for the help

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.

flat cypress
#

now i am sure that object's data refers to the variables themselves

#

and... fields too?

#

and whatever the constructor is up too

#

if it's using super() to do some shenanigans

trail nimbus
trail nimbus
#

the actual data is the stuff inside the variables

flat cypress
#

i see

#

the constructor would be more of an instruction, right?

#

since it's always called when an object is instantiated

trail nimbus
#

yes

#

"instance variable", "field", "attribute", and "property" all refer to the same thing in java
some might be different things in different languages but to java they're the same thing

flat cypress
#

cool

#

the term attributes are never used

#

you use instance variables instead, right?

#

hmm now i see the use of fields and instance variables

#

fields are dynamic within the object

#

but instance variables can be dynamically assigned values througout the program

#

so that'd be a way for you too...

#

oh

#

interesting

#

semantics!

trail nimbus
#

they're all interchangeable within the context of java

flat cypress
#

i see

trail nimbus
#

they have some different connotations

#

but ultimately they're the same thing

flat cypress
#

just like the terms instance and object

trail nimbus
#

yeah

flat cypress
#

i need to learn the purpose of them now

#

is it a bad idea to learn the semantics of it with minecraft modding?

trail nimbus
#

"field" has a focus on it being a class member
"instance variable" has a focus on it being the variable
etc

flat cypress
#

nice

#

i still need to learn the purpose of stack memory in making mods

#

like you said it's just like verbs

trail nimbus
#

what

#

that was about methods

flat cypress
#

yeah

#

methods return stack memory, no?

trail nimbus
#

no

#

methods utilize stack memory for making local variables

flat cypress
#

oh i see

trail nimbus
#

method calls exist on the callstack

#

i have no idea what "methods return stack memory" means to you, that doesn't make sense

flat cypress
#

in my head it sounds just like temporary memory

#

so the returned value of a method is not on stack

#

that open more possibilities

trail nimbus
#

if this is for making minecraft mods, these internals aren't significant to you

flat cypress
#

internals?

trail nimbus
#

you aren't going to be managing memory manually in java

trail nimbus
flat cypress
#

oh, it's because i have this itch on me that makes me wanna learn about everything in the minimum details

#

i am wanna be aware of what's happening

trail nimbus
#

learn about how to use the language first, rather than how the language works internally

flat cypress
#

i see

#

so the data that a method returns is not stack memory exactly?

#

you said callstack, but you were referring to the variables of the method?

trail nimbus
trail nimbus
#

it returns values

flat cypress
#

ah gotcha

flat cypress
#

interesting

#

that's cool

trail nimbus
#

it uses stack memory, it doesn't return it or anything like that

flat cypress
#

it's up to me to do whatever i want with the returned data

#

this makes a lot of sense

#

when you wanna use methods from a class, you need to refer to the object of that class to actually use them methods?

#

gosh now i am thinking it was a bad idea to start on python

#

i am gonna take a shower

#

in case i don't answer what you will type in a few minutes here

flat cypress
#

noice

trail nimbus
#

python's OOP is cursed, don't try to use it to equate to proper OOP languages lol

flat cypress
#

i think my mind has been corrupted by python

#

not sure if python has a venom

flat cypress
#

the fact that it was stated Everything in python is an object

#

this single statement corrupted my mind

trail nimbus
#

in java every reference type-value is an object

#

well except null

flat cypress
#

gonna keep in mind that object = data structure on the heap

trail nimbus
#

don't equate them

#

objects are stored on the heap

#

there's not really a proper reverse relation

flat cypress
#

i see

#

are you gonna stay up for more hours?

trail nimbus
#

it's 9am for me so yeha

flat cypress
#

i think i will be here playing with java for more hours

trail nimbus
#

well... hopefully

#

no guarantees

flat cypress
#

no problem

#

i need to see the methods thing you told me about

#

since it just returns values

#

does that mean it's up to me to do whatever i want with these values?

trail nimbus
#

yes

#

that's kinda how programming works

flat cypress
#

perhaps even assigning that value to an instance variable

trail nimbus
#

that's how functions work in any language

#

sure

flat cypress
#

nice

trail nimbus
#

that's up to the caller

#

the method doesn't care

flat cypress
#

gonna take a look at that, object type methods and... fields

trail nimbus
#

it gives the value and ceases to exist

flat cypress
#

it's just there to list statements innit

#

i think that's what you call it

trail nimbus
#

and give a return value

#

and manage exception handling

#

but yes grouping statements is the primary objective of functions

flat cypress
#

object type methods expect an object as a returned value, right?

#

actually, a reference to that object

trail nimbus
#

yes

flat cypress
#

oh wow, i can even have instance variables be a reference to an object

#

that's cool

trail nimbus
#

the reference exists on the stack, pointing to an object on the heap

flat cypress
#

oh shit i need to keep that in mind

trail nimbus
#

null is an all-zeros reference, similarly to how all the primitives have a 0-equivalent default value

flat cypress
#

Methods need to be called, they can't call themselves, dumbass

#

gonna put this on my monitor

trail nimbus
#

lmao

flat cypress
#

freaking python again

#

having functions and methods for some reason

trail nimbus
#

methods are just functions in objects/classes

flat cypress
#

yeah

#

the fact that i can create a function without being in a class in python corrupted my mind too

trail nimbus
#

java does have Function to simulate functions as first-class objects btw

flat cypress
#

i hope i will never have to use it

#

because i will 100% forget about it

trail nimbus
#

it's part of the functions api, it's really extensive and really useful actually

#

you'll get there

short glenBOT
flat cypress
#

alright, i will go take a shower and read some minecraft mods

#

simple mods that don't do much

#

like hmm ironchest perhaps

#

knowing what's done with methods data and instance variables is my primary focus now

#

thanks to your help, it's not a fog environment

short glenBOT
trail nimbus
#

if you get into minecraft mods right away, that'll involve integration as well

#

you may want to consider looking at more basic stuff just using built-ins if you want to focus on java first

flat cypress
#

hmm idk i think i will hop to modding

#

i always wanted to make mods since 2012

#

good old times

#

couldn't even install mods properly

trail nimbus
#

walk before you run shrugging

flat cypress
#

more like crawl before i walk

#

well i will go take a shower

#

thanks the for the help, again

short glenBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.

flat cypress
#

@trail nimbus yo just to confirm again, object = object variables, right?

#

i hope my memory hasn't gone foggy

flat cypress
#

just the term object itself

#

i even had a dream about it

#

weird

trail nimbus
#

"object variable" isn't a thing

waxen jewel
#

I think they mean variables, but type object, not primitive

flat cypress
#

i wanna be sure if my memory isn't failing me

waxen jewel
#

An object is a new instance of a class hence the new keyword. Having an variable type object, doesn't make it an object .It just makes it a null variable

flat cypress
#

oh i mean, when you use the term object

#

it refers to the instance variables, right?

waxen jewel
#

Yes, if I understand correctly

#

This is an object new ObjectClassType()

#

Did I answer?

flat cypress
#

i guess so

#

my memory is off lately

waxen jewel
#

Your not the only one 😂

flat cypress
#

i am not taking my adhd meds

#

problem is that my vitamin D levels are getting normalized

#

it makes me more energetic

#

makes adhd worse too

waxen jewel
waxen jewel
flat cypress
#

i mean vitamin d

waxen jewel
#

Or whatever those people are called that deal with meds

flat cypress
#

i am deficient on it

#

it's a combo

#

i am eating more

waxen jewel
#

Ahh ok

flat cypress
#

sleeping well

#

the more energy i have the worse it is

waxen jewel
#

Idk man. Can't help you much there

flat cypress
#

instead of my brain being at 1000 miles per hour it's 10000 miles per hour

#

it sucks

waxen jewel
#

Yea

flat cypress
#

do you mess with minecraft modding?

#

by any chance

waxen jewel
#

I once tried to make a Rainbow 7 Seige mod, but quickly gave up because I didn't feel like it

flat cypress
#

i see

waxen jewel
#

So no

#

Only plugin dev

flat cypress
#

oh now i get what you meant by object

waxen jewel
#

XD

flat cypress
#

i need to see the constructor's shenanigans

#

appears every time in minecraft modding

waxen jewel
#

Well, it is OOP for a reason

flat cypress
#

always setting up variables

#

using super(0

waxen jewel
#

I love the super keyword. The super keyword and abstract classes are my fav. Really random, but oh well

flat cypress
#

ah these and methods that i need to take care of

#

only because of this post that i realised that object's data is only instance variables

#

not methods

#

god damn you python!

waxen jewel
#

XD

flat cypress
#

minecraft mods are weird

waxen jewel
#

Python corrupted me lmao

flat cypress
#

me too

#

Everything is an object

#

this single line

waxen jewel
#

I should have went straight to the AP class instead of taking the intro (intro is requirement, but teacher said I could go to AP)

#

AP teaches Java.

#

But I have to go

flat cypress
#

darn in my software engineering course, they make us use a coding language that uses OUR language to type keywords

#

imagine the confusion

waxen jewel
#

F

flat cypress
#

i just used chat gpt and procedded to study python

#

damn it's so cool i can understand this

#

methods are my next objective

#

where the hell are they called

waxen jewel
#

I should go, but is that Static ABUSE??????

flat cypress
#

probably

flat cypress
#

the mod's author have a separated class for each iron chest material

#

i think it's because it's using fabric api

waxen jewel
#

Ahh ok

flat cypress
#

no forge shenanigans

waxen jewel
#

Now I REALLY have to goo

flat cypress
#

you have a good one

waxen jewel
#

You too

trail nimbus
trail nimbus
flat cypress
#

i think i was talking about object's data rather than what an object is itself

#

gosh did i forget everything you told me

trail nimbus
#

that would be the values held within the instance variables

#

but an "object" in concept is much more than its data

#

it's the data, the type, the methods, any other members

flat cypress
#

oh so it includes everything the class states

trail nimbus
#

all the instance stuff, yeah

flat cypress
#

thanks

short glenBOT
# flat cypress thanks

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.

flat cypress
#

idk what happened to my memory

#

feels like i was missing something

trail nimbus
#

amnesia moment

flat cypress
#

at least what i thought

trail nimbus
#

relatable tbh

flat cypress
#

ah damn skeptcism

#

this was a good kind of skepticism

trail nimbus
#

always a pain to go figure out what exactly you're forgetting yeah

flat cypress
#

it was about object's data vs object

trail nimbus
#

tetris effect

flat cypress
#

yeah i knew i had forgotten to ask something else but similar

#

there it is

#

@trail nimbus actually it's not memory

#

it could be my memory getting better

#

looks like vitamin d deficiency affects the brain more than i imagined

#

i am surprised this was not a torturing skepticism process

short glenBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.

flat cypress
#

@trail nimbus yo chris remember that thing about using classes as parameters?

#

am i referring to the object of that class or a reference to that object?

#

because if it's the object directly

#

it will make a lot of sense with what i am messing with here

#

managed to make my creative tab on minecraft

#

now i need to mess up with classes

#

oh by the way are parameters positional?

trail nimbus
#

if not, then no

flat cypress
#

nvm i think i got it

#

i just need to be sure of one little thing

#

since the parameter refers to an object that is on runtime

#

what if there are multiple instances of that object

trail nimbus
#

that's not really a thing

flat cypress
#

which one will the parameter refer to

trail nimbus
#

instance ≈ object, "multiple instance of object" just doesn't make sense

flat cypress
#

oh forgive me on that one

#

what i meant is what if there are multiple instances of a class during runtime

trail nimbus
#

im going to have to come back to this, not too free rn

flat cypress
#

unless this one is special

flat cypress
#

i will be here doing some tests

trail nimbus
#

the method doesn't see all instances

flat cypress
#

hmm this must be some minecraft shenanigans

#

or the api

#

no instances are created in classes

#

just plain parameters

#

oh i think i got it

#

makes sense

#

when you come back i will tell ya

short glenBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.

knotty totem
#

I may be late to the party but in case it hasn't been sorted out as far as I'm aware any object is stored on the heap including all its methods and variables and when you make a method call that instantiates an object you put a pointer in the stack and the object is still in the heap. (Local method primitives are put directly in the stack when the method is called though)

ionic yew
#

Objects don't contain methods. Classes do.
And while current day JVMs may indeed store classes and with them methods in the heap, the promise of Java is that where classes and methods are is none of the programmer's business. It is where the JVM finds it best to put it in.

feral ether
#

If you want to understand heap/stack things better, I'd just write some mildly complex C/C++ code, and look at the assembly output. You'll naturally start figuring out when things are in the Stack/Heap.

short glenBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.

flat cypress
flat cypress
#

i am loving java

#

and hating python

#

in my head it doesn't make sense for methods to be contained by classes since static methods exist

feral ether
#

Static methods just means you don't need an instance to call it. That's about it.

flat cypress
#

yeah

#

i need to see more about methods

#

they are a bit weird

#

it's all about the virtual machine

#

but i think objects still contain them in some way

feral ether
#

As a reflective object

flat cypress
#

gotta do some research

#

@feral ether are you sure methods belong to classes rather than objects? even if you need to refer to an object when calling its method?

#

let's go vyvanse is starting to kick in

trail nimbus
#

methods belong to classes yes, that's where they're declared

#

you call the method via the object, but the method itself isn't on the object

#

otherwise you'd need another copy of the method for every object you create, and that'd get quite expensive

flat cypress
#

hmm i need to take a look at java docs to see what they say about it

trail nimbus
#

this wouldn't be in javadocs

flat cypress
#

it's on oracle innit?

trail nimbus
#

if anything it'd be in the spec but this is more of just general OOP structure/concepts

trail nimbus
flat cypress
#

i see

#

suspicious

trail nimbus
#

ah those aren't docs, those are tutorials

#

they're just on the same subdomain as the actual docs but under a different slug

#

it's confusing but eh it works

flat cypress
#

oh so docs means something else

#

my bad

#

i thought it was something regarding to info

trail nimbus
#

docs is short for documentation, it specifically refers to formal descriptions of interfaces

#

tutorials have a similar purpose but they're more human and less formal

flat cypress
#

i see

trail nimbus
#

documentation is (or, well, should be) complete, while tutorials might omit stuff for brevity

flat cypress
#

this + other researches i made still make me think that objects still contain methods but they are a bit special

#

relating to the virtual machine

#

i know it's aboslutely weird for me to get into these little details

#

it's a curse i have to face

#

it happens for literally anything i learn

trail nimbus
#

i think there's an issue with ambiguous language here, like what does it mean for something to "contain" a method here

flat cypress
#

yeah indeed

#

i even searched more about volatile and non-volatile memory

#

due to this

trail nimbus
#

the actual definition and implementation of the method belong to the class, and an instance of that class can access those methods directly

#

but nothing of the method itself is stored on the object directly

flat cypress
#

i see

#

and would that relate to the virtual machine?

trail nimbus
#

no clue sorry

flat cypress
#

hmm i wanna explore this

#

gosh i am feeling so well

#

vyvanse gives me powers

trail nimbus
#

it generally doesn't matter for actual use and i tend to explore within the language rather than its underbelly so ive got no clue about this lol

flat cypress
#

because if objects somewhat contains methods it means that there would be a special allocation in ram that would relate to the java virtual machine since methods are converted into bytecode so they can operate their functions

#

my guts and skepticism attack again

#

i need to explore more of classes as well

feral ether
flat cypress
#

wish me luck

#

this will make me study computer engineering and physics as well

#

@feral ether do you mess with minecraft coding, if i may ask?

#

i am doing some stuff with fabric api

#

loving it so far

#

i just need a better comprehension of methods

#

i even made some rules myself

feral ether
flat cypress
#

that's cool

#

and on my list

#

in the future we will talk a lot about this

#

dude this is so weird

feral ether
#

Just read Baeldung and Spring Docs. They're fantastic for learning, and your goal should be to get to the point to answer questions, not ask them.

flat cypress
#

i have no thoughts in my head

#

this is so uncommon

#

adhd breaks my legs so much

#

sorry i am just so glad i get to be relaxed at least once

#

so happy i need to share it

heady summit
# flat cypress i have no thoughts in my head

Hope you and That_Guy977... keep this conversation going. I have been following it from the beginning and have found the back n forth between you two very interesting not to mention how your genuine curiosity comes out in your questions. I find them very helpful as someone very new to Java.

flat cypress
#

thanks mate

short glenBOT
# flat cypress thanks mate

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.

flat cypress
#

i always get bothered by these details

#

if i am learning a language i will explore phonology

flat cypress
#

@heady summit alright i have a theory

#

objects contain methods, but i don't mean containing data, rather, a way of indicating that these methods will be allocated in the virtual machine and then transformed into bytecode! this makes a lot of sense in my head, i am probably wrong, but it's interesting anyway

#

this would make a lot of sense of static methods

#

the point of static is so you don't load stuff every time innit?

#

this might be it

#

i mean i have seen lots of codes using static as a bridge

#

which's interesting

#

the point of static methods would be so you wouldn't need to create an object just for that method, meaning that you would still need an object to use the method due to memory allocation, that's cool!

#

less objects less allocations

#

perhaps C++ and C will have more of this

feral ether
flat cypress
#

does every single coding language have a virtual machine?

#

a specific allocation just for methods

feral ether
#

No

#

The JVM could also be called the Java Runtime.

flat cypress
#

oh i had the word at the tip of my tongue

feral ether
#

Interpreted Languages will have a Runtime (or VM) of some sort. Java is both interpreted (Java Bytecode) and compiled (Java -> Java Bytecode)

flat cypress
#

C++ doesn't have delays

#

it goes straight up to the objective

#

yes interpreted and compiled

#

thanks

short glenBOT
# flat cypress thanks

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.

flat cypress
#

ah btw when i said that an object contains methods

#

did it sound like i was referring to methods as data?

#

the usage of contain in java

#

not sure if it relates to data

#

that'd be awkward

feral ether
#

Methods are meta-data on the class

flat cypress
#

alright let me rephrase everything: classes are non-volatile since there is no allocation on RAM, objects are the allocation of the instructions stated in classes, methods are allocated in the virtual machine, converted into bytecode and then processed afterwards. So I see "Object" as memory allocation. Objects don't contain methods as data, rather, it's an indication that these methods will be allocated, unlike static methods. Static methods are used mostly when you want to use a method stated in a different class without having to instantiate that class first in order to make it get allocated. In the end, you'd be calling a method without the need of instantiating the origin class, optimizing the whole allocation process.

#

so proud i made this text

feral ether
flat cypress
#

alright, just wanted to post my text, it's from a thread

feral ether
#

No worries

#

You're trying to think of Java as a C/C++ language with Stacks/Heaps, when in reality Java is just a Runtime. Think of how a runtime would behave, instead of a Native Application.

flat cypress
flat cypress
#

@feral ether i took a good read on the article, it's really interesting

#

correct me if i am wrong. in interpretated languages, such as python, the heap memory would be a bit different wouldn't it?

#

more limited

#

with python using a lot of garbage collector of unnused objects

feral ether
#

Java is also Garbage Collected

flat cypress
#

i am liking all of this

#

so many interesting things

flat cypress
#

@feral ether oh NICE! the article confirmed an idea i had in mind

#

this is really cool

#

i wonder why people don't care much about memory allocation

hushed quest
#

tldr:
all methods, field declarations, etc are shared across instances and only exist once in memory, but the values in the instance fields are individually stored for each instance. the nonstatic methods then get that instance, which they can use to do what they want to do. a class with a field “text” and a method “print” that prints out the text variable will only store the print method once in memory, but when you call the print method, that instance with the individual text value will be passed to it, and it will print it

trail nimbus
trail nimbus
#

if you aren't super into it, the conversation just kinda stops at "garbage collection" or "reference counting"

potent leaf
#

for java, I believe unsafe is pretty much the only use-case of manual memory management I know of

short glenBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.

feral ether
feral ether
# potent leaf for java, I believe ``unsafe`` is pretty much the only use-case of manual memory...

If you're talking about the Unsafe class, that really is not part of the Java Spec, and is more a hacky workaround that power users/frameworks can leverage. I don't even know if it is available in Java 17, and I think you have to use the security manager to get it in 11.

Manual Memory Management is mostly done through JNI/JNA, with the Foreign Function/Foreign Memory JEPs targeting future versions of Java.