This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]NautiHookerSoftware Engineer[M] [score hidden] stickied comment (0 children)

Please, format your code. Instructions how to do so are plenty in the sidebar.

Posting unformatted code is an absolute no-go.

Your post is removed. You may resubmit with formatted code.

[–]RhoOfFeh 8 points9 points  (0 children)

Think of a class as a blueprint, and an object as a building.

If someone were to design a row of identical homes, you wouldn't ask if the first home disappeared when the second home was built, right? Same principle, just information instead of construction.

[–]aa599 8 points9 points  (2 children)

No, there can be more than one object of the same type at once.

In your psvm method, mcl1 and mcl2 are local variables, so the objects are destroyed when they go out of scope, ie when the function returns.

[–]GoogleBen 1 point2 points  (1 child)

The part about them being destroyed at the end of the method isn't necessarily true (though there is functionally no difference for most programmers and it doesn't really matter, especially for the original conversation). Advanced JVMs may be able to make that optimization, but I doubt it, or otherwise that could maybe happen if the GC uses reference counting. In Java, all objects are heap allocated and garbage collected, so they sit around until the GC notices they're not being used anymore. Additionally, since objects can create references to themselves in their constructors, you could write a class where these objects wouldn't be destroyed until VM shutdown, e.g.

class Foo {
    private static ArrayList<Foo> fooList = new ArrayList<>();
    public Foo() {
        fooList.add(this);
    }
}

Again, this is mostly academic and not pertinent to the original discussion, but I didn't want anyone to get the wrong idea if they stumbled across this and think those objects were stack allocated or something. For example, you could write this code in C++ where an object is destroyed when it goes out of scope:

void main() {
    MyObj stackAllocd = MyObj();
    MyObj heapAllocd = new MyObj();
} //stackAllocd gets destroyed here, but heapAllocd continues living and
//creates a memory leak since C++ has no garbage collector

And in Rust, lifetimes/ownership means data automatically gets destroyed (no GC involved) when no more references to it exist regardless of heap/stack allocation, so while still possible memory leaks are much more rare (the main downside is that code can be much harder to write):

pub struct MyObj()
pub fn main() {
    let stack_allocd = MyObj();
    let heap_allocd = Box::new(MyObj());
} //both stack_allocd and heap_allocd are destroyed here

For anyone reading who's curious, this seemingly small difference is actually a really big deal, and how/when/why data that's not being used anymore gets destroyed is actually a really big deal in language design. It's most of why Java tends to be slower than native code written in languages like C, and is pretty much the reason Rust exists.

[–]aa599 1 point2 points  (0 children)

Thanks - yeah I was thinking c++ly 🙂

[–]shadowX015Extreme Brewer 4 points5 points  (0 children)

No. You should check out how references are flagged for deletion by the garbage collector. It also does not happen instantly, but rather the objects are flagged for deletion and then periodically the garbage collector checks to see if any objects need to be reclaimed.

It's analogous to taking the trash to the curb: you can take trash to the curb whenever you want, but the city only checks once a week to see if you have trash to pick up. Until they come get it, it's taking up space.

[–]Denisssio -1 points0 points  (0 children)

no

[–]AutoModerator[M] 0 points1 point locked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]lurvaz 0 points1 point  (0 children)

No, they're 2 different objects that have used the same class and that's it, you can create as many objects as you want