0

1)Why java is stored in RAM and not stored in hard disk. 2)And what is the benefit of static variable over normal variable. Whether normal variable occupy more memory space since objects of classes have it's own copy.

3
  • Your first question is unclear and it seems you already know the answer to your second question. Commented Jul 11, 2010 at 7:17
  • All running programs go on the hard disk. Only one copy of static varibale created for all objects in memory.. Commented Jul 11, 2010 at 7:21
  • 1
    @Ram Bhat: No, running programs are not on disk. Commented Jul 11, 2010 at 7:33

3 Answers 3

4
  • Java is not "not stored in hard disk". The Java interpreter is loaded from permanent storage. As are the class files that make up your Java program. At run time, everything must be in RAM (well, disregarding paging) to be used by the processor - that's the way computers work!
  • If a variable is static, there will be either one or zero copies of it in memory. If it is an instance variable, you will have one copy per instance of the class. So yes, a static variable will use less memory in total if the class is instantiated more than once.
Sign up to request clarification or add additional context in comments.

Comments

2

Why java is stored in RAM and not stored in hard disk

If you're talking about variables, storing variables in RAM allows for fast read/write access, orders of magnitude faster than disk. Java can access disk as well.

what is the benefit of static variable over normal variable.

Static variables are not tied to a particular instance of a class, so you can access without having to create an object, but that static variable will be shared among every piece of code that has a reference to it.

Whether normal variable occupy more memory space since objects of classes have it's own copy.

Each time you instantiate an object it will take space in memory. Sometimes it's exactly what you need.

Example:

public class Bicycle{

    private int cadence;
    private int gear;
    private int speed;

    // add an instance variable for the object ID
    private int id;

    // add a class variable for the number of Bicycle objects instantiated
    private static int numberOfBicycles = 0;
    ......
}

Each time you do a new Bicycle() you will be creating a new object (thus using more memory) with all its attributes except for numberOfBicycles, there will be only one of that attribute for all instances of Bicycle.

Comments

1

All running programs are stored in memory regardless of programming language. I'll assume you mean why use .class files instead of .exes. That is for portability, the runtime basically translates the .class files into a .exe at runtime so the same binary can be run on any platform rather than having to provide a different file for each platform.

Yes, instance variables use more memory than class variables since each instance has its own copy. Static variables have all instances share the same copy. If you consider a person, each person has their own name (instance variable) while the # of eyes people have is constant for everyone, barring birth defects and accidents (class variable). Class and instance variables serve very different purposes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.