• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Does each java object instance store the method details?

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

For one class, we can create multiple java instances in JVM heap space, my doubt, does instance store method details in it?
Or method details are stored at common place so that each instance will use them for invoking ?

Thanks in advance.
 
Marshal
Posts: 74348
334
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is one of those things you don't need to know.

All methods are stored in a Class<T> object corresponding to the class the object was created from.
 
Saloon Keeper
Posts: 24501
167
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:That is one of those things you don't need to know.



But no.

Java doesn't produce self-modifying code, so the code for one method instance would be 100% indentical for any other instance of that method. Why waste machine resources duplicating something when you can share a common copy of it?
 
Saloon Keeper
Posts: 1609
52
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:

Campbell Ritchie wrote:That is one of those things you don't need to know.



But no.

Java doesn't produce self-modifying code, so the code for one method instance would be 100% indentical for any other instance of that method. Why waste machine resources duplicating something when you can share a common copy of it?



When I was VERY first introduced to OOPS, a VERY LONG TIME AGO, the presentations made it seem like a copy of each and every method was stored along with each object instance.
"What a terrible waste of memory" I thought.
I was very quickly cleared of this misconception, and looked back and laughed at it.

Then I came to Python in 2021, and realized that you can add methods, remove methods and override methods PER INSTANCE, sort of like I had originally mistakenly imagined OOPS to be before I knew anything.

So, someone coming to Java from a "Lots of Python" background, which isn't very uncommon, could be bringing over some very weird ideas with them.

No evidence that is where the OP is coming from, but there is no shortage of Primarily Python People wandering into Java nowadays.
 
Tim Holloway
Saloon Keeper
Posts: 24501
167
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ha! Forget Python - think about JavaScript!

The notable thing about both Python and JavaScript is that their OOP implementations are based on dictionaries and those dictionaries are mutable at will.

Java isn't merely strongly-typed, it's rigidly-typed, so you cannot add/remove/modify Java methods.
 
Jesse Silverman
Saloon Keeper
Posts: 1609
52
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:Ha! Forget Python - think about JavaScript!

The notable thing about both Python and JavaScript is that their OOP implementations are based on dictionaries and those dictionaries are mutable at will.



Yeah.  Everyone wants to know that you are familiar with OOPS principles, but the way they are expressed in Java or Python/JavaScript is pretty different compared to the relatively minor but still important details in the C++/Java/C# models.

So many jobs that are "Java jobs" also require either Python or JavaScript, so it is important to me personally, but the point was to remember that people "new to Java" but having a lot of experience in JavaScript and Python are going to have some strongly-wired concepts of what is "normal OOPS" that seem pretty bizarre to someone coming from an all-Java/C++/C# background.

I've gotten used to the idea (not fully internalized) that Dynamism is elevated to a religious principle in those languages, compared to the ones I am more used to where "I want to know exactly what I have and what it is safe to do with it without uncertainty" is the highest principle.
 
Sheriff
Posts: 22504
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:Ha! Forget Python - think about JavaScript!


Unless you use prototypes. Those are similar to class objects, and you can add and remove functions ("methods") at will. These are not copied for each instance.

When you use TypeScript, each method is compiled to a prototype function, to prevent having a copy per instance. That's great, but it does lead to exposed internals. Every field, whether it's private, protected or public, is fully available in the compiled JavaScript. So if every method. That's the only way to let prototype functions access them. There is a way to work around this that I found on StackOverflow. That's to not add methods, but to add [tt]readonly[tt/] properties with functions as their types. You set those in the constructor, where it can capture any local variable that is defined in the constructor. You end up with better encapsulation, but at the cost of copying methods for each instance.

In both cases you can still add or overwrite any "method" in the compiled JavaScript of course...
 
Jesse Silverman
Saloon Keeper
Posts: 1609
52
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On interviews where they have already got to know my background, they declare I'd hate JavaScript but would be okay with TypeScript in their opinion.

Prototypes are specific to TypeScript?

Such a high percentage of jobs require JavaScript as a second language it is unclear how long I can put it off.

I do feel it would be a bit of a culture shock, despite getting somewhat comfortable with Python's Hyper-Dynamism.

I now have a new term for people like me, apparently I like Rigidly-Typed languages.
Sounds inflexible, but when you hand me a reference to a class or an implementation of an interface, I sure like to think I know exactly what its data and methods are, at least.

I feel weird knowing a particular Dog object instance might just happen to know how to fly or emit settlement statements.  Sort of cool on the weekends, but out-of-my-comfort-zone at work.
 
Tim Holloway
Saloon Keeper
Posts: 24501
167
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's some irony for you. I've been working on this jewel this afternoon:
 
Rob Spoor
Sheriff
Posts: 22504
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesse Silverman wrote:On interviews where they have already got to know my background, they declare I'd hate JavaScript but would be okay with TypeScript in their opinion.

Prototypes are specific to TypeScript?


No, they are JavaScript, not TypeScript. Usually, TypeScript is compiled to JavaScript before it can be run.

In TypeScript you program almost as you'd do in Java - create a class with private, protected and public members. I love its interfaces more though, they are like interfaces in Go. There is no need to explicitly specify an object implements an interface; if the object has the correct properties, it automatically implements the interface.

Small example:


@Tim: ah yes, the anonymous sub class. That's as close as Java comes to overriding methods per instance.

That VolleyLog.wtf is appropriate to this code
 
Jesse Silverman
Saloon Keeper
Posts: 1609
52
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:Here's some irony for you. I've been working on this jewel this afternoon:



Man, you are actually using everything we ever talk about all the time!

The reason that you need the try-catch in the snippet above is because the parameter to .getBytes() need not be a compile time constant String, or it might be something exotic and not supported everywhere, so that Java decided to make it a Checked Exception, right?

In reality, if I understand .getBytes() correctly, the only way you can get in that catch block is if you had spelled "utf-8" wrongly, or bizarrely, if it was running on some Java version that failed to support "utf-8" as too exotic?

 
Jesse Silverman
Saloon Keeper
Posts: 1609
52
Eclipse IDE Postgres Database C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dang it Tim, between me trying to recover from CharsetFever and ExecptionHandlingFever, both of which you've been witness to...

If you called .getBytes() with a parameter of java.nio.charset.StandardCharsets.UTF_8
https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/nio/charset/StandardCharsets.html

could you then chuck the try-catch into the BitBucket??

Bonus humor points, "Would it make that method faster?"
 
Jesse Silverman
Saloon Keeper
Posts: 1609
52
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wait, I wrote that all as a joke because I needed a laugh at the end of a Long Day.

But since one of my ongoing goals in life is to find lots of ways to Avoid Checked Exceptions so my life can become Streamier, let's actually look at that one?
 
Tim Holloway
Saloon Keeper
Posts: 24501
167
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The primary concern I had with the try/catch was to shut the computer up. Although there are 2 possible ways that getBytes() should be able to fail: one is by requesting an unavailable (or mis-spelled) codepage. The other is by attempting to convert a character that isn't mapped to the code page (unlikely for UTF-8), but consider a vanilla ASCII (ASCII, Not ASCIIZ) output for "ü".

Since I know exactly what data is going to pass through here, I know that - absent corruption in the database that feeds it - the exception will never occur, I'm willing to Worry About It Later. But in the mean time: SHUT UP, COMPILER!
 
Jesse Silverman
Saloon Keeper
Posts: 1609
52
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, but doesn't this Overload Not Need No Stinking Try-Catch?

public byte[] getBytes​(Charset charset)
Encodes this String into a sequence of bytes using the given charset, storing the result into a new byte array.
This method always replaces malformed-input and unmappable-character sequences with this charset's default replacement byte array. The CharsetEncoder class should be used when more control over the encoding process is required.

Parameters:
charset - The Charset to be used to encode the String
Returns:
The resultant byte array
Since:
1.6



You don't get that exception thrown because it sees some odd weird chars it doesn't know how to map, only if you can't convert to a Charset by the name you referenced.
I thought all the overloads do that, and the difference is just in whether you pass a known valid Charset in as a parm or insist on passing in a name of one you hope exists as a String and praying at runtime??
 
Jesse Silverman
Saloon Keeper
Posts: 1609
52
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My (possibly mis-) understanding is that you need to deal with this bugger if and only if you are using an exotic charset encoding that is not one of the Standard ones?

UnsupportedEncodingException - If the named charset is not supported
 
Tim Holloway
Saloon Keeper
Posts: 24501
167
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I looked at the source for java.lang.String, but the method that throws that Exception is native code and I wasn't ambitious enough to look for the JNI side of String. All I knew was that it's a Checked Exception, so I had to explicitly deal with it.
 
Jesse Silverman
Saloon Keeper
Posts: 1609
52
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not a big deal here because no lambdas, streams etc. are involved, but I think if you switched to the other overload the checked exception would completely disappear.
 
Rob Spoor
Sheriff
Posts: 22504
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jesse is right. The getBytes(String) method may be called with an invalid charset name like "foo". If that happens, a checked exception is thrown. The getBytes(Charset) method can be called with StandardCharsets.UTF8. You get type safety, no possibility for typos, and most importantly - no checked exceptions.

However, this is Android code (the Toast and Log.e are giveaways). I'm not familiar enough with it, but if I recall correctly, Android doesn't provide a full JSE API. That means that this method may simply not be available.
 
Tim Holloway
Saloon Keeper
Posts: 24501
167
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To tell the truth, I didn't analyze it that closely. I was attempting to take code that wat JSON-in/JSON out and make it JSON-in/text out mostly by copy-and-paste from That Other Website and by the time I got down to that part I'd spent 90 minutes trying to get the function overloads to parse properly (there was a difference in parameter types way up top that threw me).  So it could just be naïveté on the part of the original author, since I can't see that eliminating the utf-8 default method as being a worthwhile reduction for Android's sake.

Anyway, at the moment I'm trying to get a background process running, so I'm still angry. Between Callback Hell worthy of JavaScript and paradigm obsolescence worth of Microsoft, Android is a disgrace to the concept that Java should be a stable and civilized environment. I spend at least as much time trying to figure out which methodology is least obsolete as I do on finding out how I'm supposed to use the version-of-the-week.
 
reply
    Bookmark Topic Watch Topic
  • New Topic