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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T11:37:02+00:00 2026-06-16T11:37:02+00:00

Okay, so, for example, let’s say I have an abstract class called Vehicle. The

  • 0

Okay, so, for example, let’s say I have an abstract class called “Vehicle”. The Vehicle class, has, among other things, a static variable called wheels, which is not initialized. What I want to do is have other subclasses extending from the Vehicle class, like “Motorcycle”, and “Truck”, and in these subclasses, have the wheels initialized.

Code:

public abstract class Vehicle {
    static int wheels; //number of wheels on the vehicle
}

But the below doesn’t work:

public class Motorcycle extends Vehicle {
    wheels = 2;
}

Is there a way to do this effectively?

EDIT:
Thank you to all the people who replied so far. I get that making instances is probably a better way to go than to put them all in separate classes, but I don’t get the “static” part of java perfectly, so I need a little help here.

What I’m trying to do for my program is have separate sprites for the Motorcycle and Truck classes, and I want them to be static so that I won’t have to reload the image every time I create an instance of a Motorcycle or Truck. Other than that, though, they’ll have almost identical properties to each other, which is why they’ll both be extending from the Vehicle superclass.

The only other way I can see this being done is by just not declaring the sprite variable at the Vehicle class, but at the Motorcycle/Truck class, like below:

public abstract class Vehicle {
//Other coding
}

public class Motorcycle extends Vehicle {
static BufferedImage sprite = //initialize image
//Other coding
}

public class Truck extends Vehicle {
static BufferedImage sprite = //initialize image
//Other coding
}
  • 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-16T11:37:04+00:00Added an answer on June 16, 2026 at 11:37 am

    If ‘wheels’ is static, there is only one and it will apply to all vehicles at the same time. So tricycle, a motorcycle, an 18-wheeler truck and a Ford will all have the same number of wheels.

    That doesn’t make sense to me. It would be better to have ‘wheels’ be an instance variable that is in the parent class but each subclass sets appropriately.

    But you can try

    Vehicle.wheels = 2;
    

    NOTE: I’m adding to my answer since you added to your question.

    I like your idea of having statics in each of the subclasses. But you should make them private. Then put an abstract method in the parent class (Vehicle) like

    public abstract BufferedImage getSprite();
    

    Then each direct subclass has to have the same method and it can return the private static variable.

    Make the variable static so you only have to load them once. Make them private so that code outside the class itself can’t fool with it and introduce bugs. You could make them ‘final’ if possible so the code in the class itself can’t change it after the fact and introduce bugs. (A ‘final’ variable can’t have its value changed but the contents of its value can change. So ‘final’ isn’t a wonderful as it could be.)

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

Sidebar

Related Questions

Okay, let's say you have something like this: <span class=image style=background-image: url('http://www.example.com/images/image1.png')></span> Every CSS
Let's say I have a base class template MyBase: template <class T> class MyBase{
Let's say my SQL database contains a table called 'Customer'. This Customer has an
let's say I have a very long line of inline styling, for example: <input
Let's say I have the following method: public static int CountNonNullMembers<T>(this IEnumerable<T> enumerable) {
okay, so i have a text file with example content below line1with some random
Okay, so I already have a script to let users enter tags but I
let's say when I'm comparing values in ruby, i have a value in mind
I have one class, let's call it ClassA , and a bunch of subclasses
Okay now let me give an example date : 15.12.2012 00:16:39 Now this will

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.