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

  • Home
  • SEARCH
  • 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 6803287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:19:34+00:00 2026-05-26T19:19:34+00:00

I’m facing strange OOP behaviour (IMO) in android and maybe in Java in general…

  • 0

I’m facing strange OOP behaviour (IMO) in android and maybe in Java in general…
I have an activity and inside of it I instantiate an object of CustomSimpleCursorAdapter which is a class extending SimpleCursorAdapter. In my custom class I have some member variables added.
When I create an object of that class I want to be able to access a member variable of newly created object. My OOP experience was telling me that the following should suffice:

ListAdapter listAdapter = new TrainingsListCursorAdapter(...);
listAdapter.currentWidth = 100;

NOTE: currentWith variable is declared public…

(Eclipse) didn’t want to compile, so I thought maybe a public setter setCurrentWidth()in the custom class would solve it. I declared a member variable private and my setter method public….

ListAdapter listAdapter = new TrainingsListCursorAdapter(...);
listAdapter.setCurrentWidth(100);

Eclipse wasn’t pleased either… I knew it was a long shot, but when you face strange behaviour, you start to behave strange too :)))

The correction that eclipse was intrusively offering and in the end works! is the following code:

((TrainingsListCursorAdapter) listAdapter).setOrientationWidth(100);

Could someone please explain me this syntax and need for such casting?!
…and what’s wrong with:

  • instantiate an object
  • access it public setters/getters or directly member variables
  • 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-26T19:19:35+00:00Added an answer on May 26, 2026 at 7:19 pm

    It’s not particularly strange at all. The variable listAdapter is just of type ListAdapter. The compiler couldn’t care less what assignments have occurred on previous lines. If you try to access a member which isn’t known via ListAdapter, the compiler will correctly complain.

    A simpler fix than casting would be to declare the variable as being the type you need it to be in order to access your specific members:

    TrainingsListCursorAdapter listAdapter = new TrainingsListCursorAdapter(...);
    listAdapter.currentWidth = 100;
    

    or

    TrainingsListCursorAdapter listAdapter = new TrainingsListCursorAdapter(...);
    listAdapter.setOrientationWidth(100);
    

    So to answer your final point – there’s nothing wrong with instantiating an object and accessing public members. There is something wrong with trying to do so via a variable of a type which doesn’t know about those members.

    To give a simpler example in common types, you can’t write:

    Object text = "hello";
    int length = text.length();
    

    because length() is a method on Object, it’s only on String. When the compiler sees the expression text.length(), it looks at the declared type of the variable text (i.e. Object in this case) and looks for the length() method on that type.

    So this is what you’d do instead to make the above code work:

    String text = "hello";
    int length = text.length();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have thousands of HTML files to process using Groovy/Java and I need to
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am trying to loop through a bunch of documents I have to put

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.