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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:33:47+00:00 2026-06-04T13:33:47+00:00

I’m trying to write a custom UI component (on android) that will display a

  • 0

I’m trying to write a custom UI component (on android) that will display a text edit control along with two buttons which can be clicked up or down. Up increments the number in the edit control and down decrements it.

I’d like this class to be generic on the numeric type i.e. i’d like to specialize the class for int and float types.

I’ve been reading this nice Java Generics Tutorial but my particular implementation doesn’t seem to compile. Here’s what I have got:

public class NumberPicker<T> extends LinearLayout implements OnClickListener,
        OnFocusChangeListener, OnLongClickListener {

    public interface OnChangedListener<T> {
        void onChanged(NumberPicker<T> picker, T oldVal, T newVal);
    }

    public interface Formatter<T> {
        String toString(T value);
    }

    // Error: Cannot make a static reference to the non-static type T
    public static final NumberPicker.Formatter<T> TWO_DIGIT_FORMATTER =
            new NumberPicker.Formatter<T>() {
                public String toString(T value) {
                        // Do something with T value
                }
        };
}

I’m getting the following compiler error in the declaration of the TWO_DIGIT_FORMATTER:

Cannot make a static reference to the non-static type T

Can someone please help me with the syntax. Coming from C++ I understand generic well enough and am aware of some of the differences between the two. I just need help with the syntax.

I tried changing to:

        // "Syntax error on token "int", Dimensions expected after this token"
    public static final NumberPicker.Formatter<int> TWO_DIGIT_FORMATTER =
            new NumberPicker.Formatter<int>() {
                public String toString(int value) {
                    return mFmt.toString();
                }
        };

This gives “Syntax error on token “int”, Dimensions expected after this token”

  • 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-04T13:33:48+00:00Added an answer on June 4, 2026 at 1:33 pm

    The T parameter belongs to a NumberPicker instance, each instance can have a different value to it, so it can’t be relied on in a static field of the class.

    Nor can it be referenced in the public interface Formatter<T>, as interface members are by default static.

    One way to work around this is:

    1. TWO_DIGIT_FORMATTER, being a constant should have the parameter bound. When you think about it, an unbound parameter doesn’t really make much sense in this situation.
    2. Formatter can have its own separate parameter, checked when you pass it to a NumberPicker instance. (This is already done implicitly in OnChangedListener.)

    So your code will be something like this:

    public class NumberPicker<T>  {
    
        public interface OnChangedListener<Y> { //
            void onChanged(NumberPicker<Y> picker, Y oldVal, Y newVal);
        }
    
        public interface Formatter<X> {
            String toString(X value);
        }
    
        public void setFormatter( Formatter<T> formatter ) { ... }
    
        public static final NumberPicker.Formatter<? extends Number> TWO_DIGIT_FORMATTER =
            new NumberPicker.Formatter<Number>() {
                public String toString(Number value) {
                        // Do something with T value
                }
            };
    
    }
    

    I replaced T with X and Y in the interface definitions to avoid confusion. The parameter of the interfaces is completely separate from the parameter of NumberPicker By giving them different names this becomes readily apparent.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

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.