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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:53:00+00:00 2026-05-26T08:53:00+00:00

I have made the static class below so any class may access any of

  • 0

I have made the static class below so any class may access any of my lejos robot’s sensor methods without me having to make an instance for each class.

However whenever I call a method such as StandardRobot.motorA.setPower(100) my robot crashes. When I use exactly the same class and make a local instance of it this works fine. Why is this? Both times my code compiles fine and fails at runtime.

import lejos.nxt.*;

public class StandardRobot {

    public static ColorSensor colourSensor;
    public static TouchSensor touchSensor;
    public static UltrasonicSensor ultrasonicSensor;
    public static NXTMotor motorA, motorB;

    public StandardRobot() {
        // instantiate sensors
        ultrasonicSensor = new UltrasonicSensor(SensorPort.S1);
        colourSensor = new ColorSensor(SensorPort.S2);
        touchSensor = new TouchSensor(SensorPort.S4);

        //instantiate motors
        motorA = new NXTMotor(MotorPort.A);
        motorB = new NXTMotor(MotorPort.B);
    }
}
  • 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-26T08:53:01+00:00Added an answer on May 26, 2026 at 8:53 am

    You’re trying to create a utility class, but your variable initialization takes place in a constructor.

    Constructors are only called when an instance is… constructed (via new).

    You need to initialize the static properties statically, either in a static initialization block, or as they’re declared.

    // Initialize static properties as they're declared.
    public static ColorSensor colourSensor = new ColorSensor(SensorPort.S2);
    
    // Or initialize in a static initialization block to do them all at once.
    public static TouchSensor touchSensor;
    // ... and the others.
    static {
        touchSensor = new TouchSensor(SensorPort.S4);
        // ... and the others.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made my custom In extension method as shown below: public static class
I have made a java swing GUI. Now I want to display a static
I have made a class which a form can inherit from and it handles
I have a class, in which i declare a static function. I register this
I've made my Logger, that logs a string, a static class with a static
This is getting made in a static void method in the main class. (class
I have a static class in which one property defines a resource manager. for
I am writing a simple Interface class as below. I need to make only
I've made a class (code below) that handles the creation of a matching quiz
I have made a SVG image, or more like mini application, for viewing graphs

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.