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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:54:16+00:00 2026-06-18T05:54:16+00:00

I have an abstract superclass that has a protected String variable. It is not

  • 0

I have an abstract superclass that has a protected String variable. It is not initialized in the superclass.

All I want to do is initialize and use it in a subclass. So, I have something like this:

    public class MySubclass extends MyAbstractSuperclass {        
        thing = "i'm a thing";
        [...]
    }

Seems simple enough, since the whole point of inheritance is reusing classes and their fields in different contexts. But that is wrong: Syntax error on token "thing", VariableDeclaratorId expected after this token.

I can get it to compile by putting brackets around the subclass initialization. What I would like to know is why the first way is wrong, and why brackets fix it. Thanks for any help.

edit: I appreciate the tip that I can fix this by using a constructor. However, I am still curious as to what is actually wrong with using a superclass field in this way, as it seems to me to be pretty intuitive. And also, why do the braces fix it, what exactly does it mean to surround a statement with braces like that outside of e.g. a loop structure?

  • 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-18T05:54:18+00:00Added an answer on June 18, 2026 at 5:54 am

    What I would like to know is why the first way is wrong, and why brackets fix it. Thanks for any help.

    public class MySubclass extends MyAbstractSuperclass {        
        thing = "i'm a thing";
        [...]
    }
    

    You try to do a statement here, set the value of the variable thing to text. You can not do statements here, you can only do variable declaration here.

    public class MySubclass extends MyAbstractSuperclass { 
        {       
            thing = "i'm a thing";
        }
        [...]
    }
    

    Now, you have a initializer block, this one is executed as a statement (before the constructor). Therefore, you can access thing, because visibility is protected. See for example here: http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html

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

Sidebar

Related Questions

I have an abstract superclass that has JPA annotations on it mapping some of
I have an abstract superclass and various subclasses. Each subclass contains a value that
I have a abstract base C# class with a couple of methods that has
I have a class with a subclass. The superclass has a Position property. The
So say I have a subclass that extends a superclass. In what scenarios do
I have an abstract superclass with a property that must be set by the
I am new to Java, need some help I have an abstract superclass that
Suppose we have abstract class A (all examples in C#) public abstract class A
I have an abstract class Class. The class Subclass extends Class. The Class abstract
I have an abstract baseclass with some protected members, but no abstract members: internal

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.