• Post Reply Bookmark Topic Watch Topic
  • New Topic

Object contains manipulation code ??  RSS feed

 
Shubham Semwal
Ranch Hand
Posts: 176
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've just started preparing for OCJP exam. I'm in the beginning phase of preparation and something's got me confused

"Object contains data and code to manipulate that data."
Is the statement right ?? as far as i know object only contains copy of instance variables.

thanks
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "code" in question would be the methods that operate on the data. Those are part of the class as well.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

"Object contains data and code to manipulate that data."

I would say that this sentence is a bit sloppy with language. It mixes up classes and objects, for example.

A class is a blueprint to create objects from - the class defines what objects (instances of the class) look like. The class contains the declaration and definition of member variables and methods.

An object, at runtime, is a block of memory that contains the values of the non-static member variables for that object. There is no copy of the bytecode of the methods there - that would be wasteful and unnecessary (the methods are exactly the same for each object of a certain class).
 
Shubham Semwal
Ranch Hand
Posts: 176
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:=

An object, at runtime, is a block of memory that contains the values of the non-static member variables for that object. There is no copy of the bytecode of the methods there - that would be wasteful and unnecessary (the methods are exactly the same for each object of a certain class).


That's what made me confused. Objects being a runtime entity has no physical memory before execution. And as far as i know class is loaded(and methods) before execution so although an object can access the methods they do not contain them. Methods gets memory in class area and objects in heap so technically speaking objects have access to manipulation code and they do not contain that code within themselves.
Just wanted to clarify
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shubham Semwal wrote:That's what made me confused. Objects being a runtime entity has no physical memory before execution. And as far as i know class is loaded(and methods) before execution so although an object can access the methods they do not contain them. Methods gets memory in class area and objects in heap so technically speaking objects have access to manipulation code and they do not contain that code within themselves.
Just wanted to clarify

It seems that you're worrying a bit too much about mechanics here: the class is the definition; the object is the thing. Since it doesn't make much sense to copy execution code (as opposed to data) for each instance, there is only one copy of any method for a given class (although it's possible that there are exceptions).

I think that instance methods differ from static ones by having an implicit this parameter added, but I'm not absolutely sure. It makes sense to me to imagine it that way though.

The details are almost certainly available in the JVM spec, but I'm afraid I'm not exactly sure where, since I've never had to worry about it (I did the SCJP exam a while back, but I don't remember it being a big issue).

Winston
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shubham Semwal wrote:
That's what made me confused. Objects being a runtime entity has no physical memory before execution. And as far as i know class is loaded(and methods) before execution so although an object can access the methods they do not contain them. Methods gets memory in class area and objects in heap so technically speaking objects have access to manipulation code and they do not contain that code within themselves.
Just wanted to clarify


In my opinion, you are *way* over complicating the statement. I think the statement is just saying that given an object reference, you can get to data, and you can call methods. It is trying to explain a characteristic of the OOP concept. I don't think that it is trying to explain the inner workings of the JVM (or any implementation details).

Henry
 
Shubham Semwal
Ranch Hand
Posts: 176
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay.. thanks for the replies..
I'm not sure if i'd be able to prepare and crack the exam in a month. I just want to try my best
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!