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

  • Home
  • SEARCH
  • 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 7869915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:27:49+00:00 2026-06-03T01:27:49+00:00

I have my main class from which i call my sub class. My sub

  • 0

I have my main class from which i call my sub class.

My sub class contains some public static variables like

    public class SubClass2 extends Main {


     public static  long a = 0;
     public static  long b = 0;
     public static  long c= 0;

    public void Analyze(int number)
    {
      b=2;

    //some code
     }
    }

Where as in main i call the object of the SubClass2.I want everytime when i make the new object of the subclass2 in main then it initializes
all the variables =0 but when i take the print statement of the variable b.It prints out like 4.It adds up the previous value with the new value.

enter image description here

  • 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-06-03T01:27:51+00:00Added an answer on June 3, 2026 at 1:27 am

    Your fields should not be declared as static in that case. This is why they’re not being initialised each time. A static field is initialised once only, then shared by every instance of the class, and depending on accessibility, also outside of the class.

    The logic that led to the value 4 must be in the code you’ve replaced with //some code, but this ins’t really relevant here.

    If for whatever reason these really should be static fields that are initialised each time an instance is instantiated, then you would have to initialise them manually in the class’s constructor. But I’d seriously question the design that leads to this situation…

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

Sidebar

Related Questions

I have a main class(which is basically a netbeans form;drag and drop) from where
I have jFrame class MainForm which contains main() , placePanel(panel) and drop down menu.
I have a generic abstract base class from which I would like to derive
Here you have my switch from my Class Essaie with the Main() : switch(c)
I have a Class(call it main class) and the method in main class calls
I have a Service class and an main Acitivity class which is supposed to
I have the following code, which uses a Unicode string class from a library
I have a class which extends JFrame and forms the GUI of my program.
I have my main application class as follows and what I would like to
The easiest implementation is when we call from single class main method other classes

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.