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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T22:25:22+00:00 2026-05-19T22:25:22+00:00

I don’t understand why I need to cast member variables to the proper type

  • 0

I don’t understand why I need to cast member variables to the proper type when their types are already declared. For example:

public class SomeClass extends SomethingElse {

  private Funky mFunkyVar;
  // a whole bunch of other variables and methods

  public void needToCast() {
    mFunkyVar = (Funky) new FunkySubClass();
  }

}

Am I making some newbie mistake or is this indeed idiomatic Java? In the above I’m assuming that FunkySubClass is indeed a subclass of Funky.

  • 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-19T22:25:23+00:00Added an answer on May 19, 2026 at 10:25 pm

    Actually you don’t have to cast it!

    Am I making some newbie mistake

    Yeap, but don’t worry… after this you wont do it again 😉

    See:

    $cat Funky.java  
    class Funky {
    }
    class FunkySubClass extends Funky { 
    }
    class SomethingElse { 
    }
    class SomeClass extends SomethingElse { 
        private Funky aFunkyVar;
        //...
        public void noNeedToCast() { 
            aFunkyVar = new FunkySubClass();
        }
    }
    
    $javac Funky.java 
    $
    

    No compilation errors.

    EDIT

    You can cast primitives and object references.

    For primitives you “have” to cast, if you want to narrow the value.

    For instance:

    int i = 255;
    byte b = ( byte ) i;
    

    If you don’t cast the compiler will warn you, “hey, you don’t really want to do that”

    ...
    byte b = i;
    ...
    Funky.java:18: possible loss of precision
    found   : int
    required: byte
            byte d = i;
    

    Casting is like telling the compiler: “hey don’t worry, I know what I’m doing, ok?”

    Of course, if you cast, and the value didn’t fit, you’ll get strange results:

    int i = 2550;
    byte b = ( byte ) i;
    System.out.println( b );
    

    prints:

    -10 
    

    You don’t need to cast when the type is wider than the other type:

    byte b = 255;
    int i = b;// no cast needed
    

    For references works in a similar fashion.

    You need to cast, when you want to go down into the class hierarchy ( narrow ) and you don’t need to cast when you want to go upper in the hierarchy ( like in your sample ).

    So if we have:

       java.lang.Object 
       |
       + -- Funky
            |
            +-- FunkySubclass
    

    You only have to cast, when you have a type upper in the hierarchy ( Object or Funky ) in this case. And you don’t have to, if you have a type lower in the hierarchy:

    void other( Object o ) { 
        // Cast needed:
        Funky f = ( Funky ) o;
        FunkySubClass fsc = ( FunkySubClass ) o;
        FunkySubClass fscII = ( FunkySubClass ) f;
        // Cast not needed:
        Object fobj = f; 
        Object fscobj = fsc; 
        Funky fparent = fsc;
    }
    

    With primitives the JVM know how to truncate the value, but with reference no. So, if the casted value is not of the target type, you’ll get a java.lang.ClassCastException at runtime. That’s the price you have to pay, when you tell the compiler “I know what I’m doing” and you don’t.

    I hope this is clear enough and don’t confuse you.

    🙂

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

Sidebar

Related Questions

Don't ask me why but I need to do the following: string ClassName =
Don't think that I'm mad, I understand how php works! That being said. I
Don't quite understand why this copy constructor is not invoked when I build with
Don't know what the term is called (substitution?), but in python if you type
don't understand: in my controller: @json = User.all.to_gmaps4rails do |user| \Title\: \#{user.email}\ end in
Don't understand, if Data.Map is and [] is. I found this out while wondering
Don't ask me why but i can't use this method because I need to
Don't quite understand determinism in the context of concurrency and parallelism in Haskell. Some
Don't know why this conversion fails. SELECT CAST('32.999' AS INT) throws the error saying
Don't need to do this right now but thinking about the future... What would

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.