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 6999697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:34:31+00:00 2026-05-27T20:34:31+00:00

I have learned that in a class you can declare variables and methods. They

  • 0

I have learned that in a class you can declare variables and methods. They can both be declared as static if that is prefered.

Now I have encountered a program example I do not really understand. After some variable declaration in the class there is a field declared as static and inside there is program code.

When is this code executed? My guess is that the code is executed like following when a new object is created:

  1. Memory allocation for the instance variables.
  2. Execution of the contructors connected to the instance variables.
  3. Exection of the static field
  4. Execution of the constructor (if “= new Constructor() is used when the object is created)

If I execute

MyCars myCars = new MyCars();

the following will happen in this order?

public class MyCars
{
    private Car volvo = new Car()
 //              (1)       (2)
    static
    {
        volvo.setNumberOfWheels = 4;
 //                  (3)
    }

    public MyCars()
    {
         volvo.setBrand = "Volvo";
         volvo.setModel = "XC70";
                 (4)
    }

Here is the original code:

public class SettingsSetter extends ListActivity
{
    private static Map<Integer,String> menuActivities=new HashMap<Integer,String>();
    private static List<BooleanSetting> settings=new ArrayList<BooleanSetting>();

    static 
    {
        menuActivities.put(R.id.app,
            Settings.ACTION_APPLICATION_SETTINGS);
        menuActivities.put(R.id.security, Settings.ACTION_SECURITY_SETTINGS);
        menuActivities.put(R.id.wireless,
            Settings.ACTION_WIRELESS_SETTINGS);
        menuActivities.put(R.id.all,
            Settings.ACTION_SETTINGS);

        settings.add(new BooleanSetting(Settings.System.INSTALL_NON_MARKET_APPS,
            "Allow non-Market app installs", true));
        settings.add(new BooleanSetting(Settings.System.HAPTIC_FEEDBACK_ENABLED,
            "Use haptic feedback", false));
        settings.add(new BooleanSetting(Settings.System.ACCELEROMETER_ROTATION,
            "Rotate based on accelerometer", false));
    }
  • 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-27T20:34:32+00:00Added an answer on May 27, 2026 at 8:34 pm

    Static initialisers are executed when the class is loaded, so before any instances are created. Your first code is in error, since volvo is an instance (non-static) variable, and the static block cannot access it. Revisiting your example:

    public class MyCars
    {
        private static Car audi = new Car()
     //                     (2)
        private Car volvo = new Car()
     //              (5)       (6)
        static
        {
            audi.setNumberOfWheels = 4;
     //                  (3)
        }
        public MyCars()
        {
             volvo.setBrand = "Volvo";
             volvo.setModel = "XC70";
     //              (7)
        }
    }
    
    MyCars myCars = new MyCars();
    // (1)                (4)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My problem is the following; I have a class that uses non-static native methods
I have learned that we can't instantiate an abstract class. But today i tested
I have an application that stores images in a database. Now I have learned
I just learned that an object can have only one ID, so I'm not
I have recently learned that you can use a neat switch statement with fallthrough
So I have learned that that the Microsoft.Jet.OLEDB.4.0 data provider for querying data sources
I'm required to write a JCE provider. I have learned that I need to
One CSS rule I have learned is that you should use the relative em
I recently learned that all stl containers have swap function: i.e. c1.swap(c2); will lead
I have learned so much from http://www.summerofnhibernate.com/ nhibernate screen casts that i wonder why

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.