Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6900599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:33:56+00:00 2026-05-27T07:33:56+00:00

For example: class A { static int i=0; static int j; static void method()

  • 0

For example:

class A {
    static int i=0;
    static int j;

   static void method() {
       // static k=0; can't use static for local variables only final is permitted
       // static int L;
    }
}

Where will these variables be stored in Java, in heap or in stack memory? How are they stored?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T07:33:57+00:00Added an answer on May 27, 2026 at 7:33 am

    Static methods (in fact all methods) as well as static variables are stored in the PermGen section of the heap, since they are part of the reflection data (class related data, not instance related). As of Java 8 PermGen has been replaced by MetaSpace and as per JEP 122 it only holds meta-data while static fields are stored in the heap.

    Note that this mostly applies to Oracle’s Hotspot JVM and others that are based on it. However, not every JVM has PermGen or Metaspace like Eclipse OpenJ9.

    Update for clarification:

    Note that only the variables and their technical values (primitives or references) are stored in PermGen space.

    If your static variable is a reference to an object that object itself is stored in the normal sections of the heap (young/old generation or survivor space). Those objects (unless they are internal objects like classes etc.) are not stored in PermGen space.

    Example:

    static int i = 1; //the value 1 is stored in the PermGen section
    static Object o = new SomeObject(); //the reference(pointer/memory address) is stored in the PermGen section, the object itself is not.
    

    A word on garbage collection:

    Do not rely on finalize() as it’s not guaranteed to run. It is totally up to the JVM to decide when to run the garbage collector and what to collect, even if an object is eligible for garbage collection.

    Of course you can set a static variable to null and thus remove the reference to the object on the heap but that doesn’t mean the garbage collector will collect it (even if there are no more references).

    Additionally finalize() is run only once, so you have to make sure it doesn’t throw exceptions or otherwise prevent the object to be collected. If you halt finalization through some exception, finalize() won’t be invoked on the same object a second time.

    A final note: how code, runtime data etc. are stored depends on the JVM which is used, i.e. HotSpot might do it differently than JRockit and this might even differ between versions of the same JVM. The above is based on HotSpot for Java 5 and 6 (those are basically the same) since at the time of answering I’d say that most people used those JVMs. Due to major changes in the memory model as of Java 8, the statements above might not be true for Java 8 HotSpot – and I didn’t check the changes of Java 7 HotSpot, so I guess the above is still true for that version, but I’m not sure here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

given public Class Example { public static void Foo< T>(int ID){} public static void
I've seen examples like this: public class MaxSeconds { public static final int MAX_SECONDS
Example: public class TestClass { public static void main(String[] args) { TestClass t =
Example class in pseudocode: class SumCalculator method calculate(int1, int2) returns int What is a
class Program { public delegate void VoidMethodDelegate(); public delegate int IntMethodDelegate(); static void Main(string[]
Can i access a method from a instance of the class? Example: class myClass
This is getting made in a static void method in the main class. (class
public class counting { private static int counter = 0; public void boolean counterCheck(){
Example 1 /** *Program Name: Cis36L0411.java *Discussion: Class -- Data Members ONLY * Method
Say suppose I have the following Java code. public class Example { public static

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.