Stack and Heap memory in Java

I am compiling few questions at one place. Can someone please give me answers?

1) I have class A and class B. if I create a instance of B in class A's method, where the memory will be created?
2) where the static methods memory will be created?
3)where the singleton class memory will be created and when will it be destroyed?
4)what is the difference between static class and singleton class. what is the base to choose?
glimlachAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sciuriwareCommented:
1) always on the heap.
2) local variables of ALL methods live always on the stack.
3) always on the heap and destroyed on an UNCERTAIN moment by the garbage collector.
4) a static can not be static, you mean a static reference to a class.
Single ton is not a special instance but a choice of design.

;JOOP!
0
sciuriwareCommented:
Additional:
1) you can see JAVA as a stock market: what ever you buy or sell,
you won't ever have the shares in your hands, only papers that tell you
how much you got.
2) static methods are just methods.
They have a different usage and deployment, but all methods are the same in nature.
3) a singleton item is an item that you prevent from being created in large numbers.
A simple example is a class with a private constructor and a public 'getInstance()'
method that controls the limited instantiation.
So ............. it's just a class.
Destroyed memory does not exist:
it is released memory and it will be made available to the empty heap,
when the garbage collector 'sees' urgence (no more free memory) or when there's lots of time available.
You should not worry about it.
4) a singleton class must keep track of its only(!) instance, without the need to create one; then you must use a static reference within the class to tell what you've got.

;JOOP!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sciuriwareCommented:
.................. I fear I'd rather told you in Dutch ...............................................
;JOOP!
0
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

glimlachAuthor Commented:
I am sorry, I may not understand your answers correctly. My questions are related to memory creation and memory destroy.
Q1. If creates on Heap then when will it destroys? in the first question, if it creates in Heap, if i also have one variable in that menthod, then where will create? will it create in Stack? what is the difference in terms of creation and memory destroying?
Q2. How can we access static methods without creating a class? will memory of those methods creates a global memory? till what time it will exists? will the memory creates at the time of calling and destroys it as soon as static method execution completes? if so, what is the difference with normal method than static method?
Q 3 & 4:. Singleton object available till the context is alive. then what is the difference between static objects memory and singleton object memory? when should i choose singleton class and when should i write a class with all static methods?
0
glimlachAuthor Commented:
I am sorry, I may not understand your answers correctly.
My questions are related to memory creation and memory deletion.
Q1. If it creates on Heap then when will this memory getdestroyed? in the first question, if it creates in Heap, if I also have one variable (eg: int )created in that method, then where will the memory for this "int" variable be created? will it create in Stack? what is the difference in terms of these memory creation and memory destroy mechanism?
Q2. How can we access static methods without creating a class? will memory of those methods created at global memory location? till what time it will exists? will the memory creates at the time of static method calling and destroys it as soon as static method execution completed? if so, what is the difference with normal methods than static methods?
Q 3 & 4:. Singleton object available till the context is alive. then what is the difference between static objects memory and singleton object memory? when should I choose singleton class and when should i write a class with all static methods?
0
sciuriwareCommented:
Q1. You will NOT know exactly when the 'destroyed' object will be 'finalised()' and
added to the free list.
Local variables in methods are ALWAYS created on the thread's stack.
The stack area is released EXACTLY at the return from method.
Q2.You MUST access a static method without creating an object, or ignoring
an existing one by calling:   class.method();
You will not create global memory by calling static methods.
There is no difference between the local memory of methods of any kind.
Q34: a class with only static methods is only a place holder for global methods.
E.g.: the Math class.
;JOOP!

P.S.: why did you close and give points if you go on asking questions?
0
glimlachAuthor Commented:
Sorry I did it mistakenly. I was not clear about the answers after that.
Thanks for the reply. however I still have few doubts on the same issue.
your answers: 1)always on the heap.
2) Local variables in methods are ALWAYS created on the thread's stack.
My questions:: what does it mean? I read that threads will access same heap memory but not stack memory.
public void test(){
B b = new B()
}
If i create B object inside a method and if two threads access this method one by one. then two variables will be created separately. if threads access same heap memory, it should use the single object right? but is it so happening in the above example if two threads access one another?

Your Answers:
You MUST access a static method without creating an object, or ignoring
an existing one by calling:   class.method();
My Questions:
I know that. Buy Why should call like that? why should i create static methods instead of normal method that invoking with object instance?

Your Answers:
a class with only static methods is only a place holder for global methods.
E.g.: the Math class.
My Questions:
I know that. That is not my question. Ok forget about memory creation part.
Singleton object available till the context is alive. then what is the difference between static objects  and singleton object? (I know how to create singleton class)
There are classes with all static methods and there are singleton classes.  when should I choose singleton class and when should i write a class with all static methods? and Why?
AND how the memory creation and it's life time of these two type of classes happens.
Sorry dont mind. I know java well how to create static classes, singleton classes, design patterns etc. but I am confused about internal mechanism of how they are creating, location, when do we have to write static class and singleton classes
0
sciuriwareCommented:
As 2 threads must be able to call methods in parallel, they have their own stack area.
And they will create separate objects in the same heap area.

You need to create a static method if you got no object (yet).
And in case of a singleton class you MUST call a static method because
you may not call the constructor.

>>> There are classes with all static methods and there are singleton classes.  when should I choose singleton class and when should i write a class with all static methods? and Why?

The Math class answers one question, a GUI creating class for your GUI should
be a singleton for obvious reasons.
And the two have no relationship.

The Math class has no life time, it is there all the time: no objects.
A Singleton may only be destroyed if the last (and only) reference to it
goes to null; that reference will also be a static variable inside the class,
so you need a static method 'destroy()' or such.

You may give me another 1000 points for this (reopen the question and give it A level).

;JOOP!
0
glimlachAuthor Commented:
ok. I'll re open and give rating if you give me answers that wont create me any more questions.

The Math class has no life time,
 it is there all the time:-- Where Sir?

 I dont think your answers for last 3 answers are really having complete/clear answers. your explanation (eg: You need to create a static method if you got no object (yet).) is correct but not answers to my questions.

Your first answer was pretty clear. Thank you.
0
sciuriwareCommented:
All classes that are loaded into the java virtual machine 'live' in  the class administration (only).

This is a simple singleton class:

public class Singleton
{
     private Singleton(){ ................ }   // inaccessible constructor.
     private static Singleton me = null;  // reference to the olny(!) instance.
     public static Singleton getInstance()
     {
           if(me == null) { me = new Singleton(); }
           return(me);
     }
     public static destroy()
     {
            me = null;
     }
}

;JOOP!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.