Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Object contains manipulation code ??

 
Ranch Hand
Posts: 176
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 43081
77
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
When it is used for evil, then watch out! When it is used for good, then things are much nicer. Like this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic