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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:02:02+00:00 2026-06-11T17:02:02+00:00

Trying to create a static field with a generic type doesn’t compile: class MyClass

  • 0

Trying to create a static field with a generic type doesn’t compile:

class MyClass {

    public static Function<Z, Z> blargh = new Function<Z, Z>() {
        public Z apply(Z a) {
            return a;
        }
    };
}

Eclipse says:

Multiple markers at this line
    - Z cannot be resolved to a type
    - Z cannot be resolved to a type
    - Z cannot be resolved to a type
    - Z cannot be resolved to a type
    - The type new Function<Z,Z>(){} must implement the inherited 
     abstract method Function<Z,Z>.apply(Z)

but replacing all the Zs with a concrete type works just fine:

static Function<Integer, Integer> blargh = new Function<Integer, Integer>() {
    public Integer apply(Integer a) {
        return a;
    }
};

What’s going on here?


Context:

  1. I was originally trying to figure out why this code uses a method instead of a field:

    public static <T extends Throwable> F<T, String> eMessage() {
      return new F<T, String>() {
        public String f(final Throwable t) {
          return t.getMessage();
        }
      };
    }
    

    Maybe it’s to overcome this restriction?

  2. the Function type is from Google’s guava library.

  • 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-11T17:02:03+00:00Added an answer on June 11, 2026 at 5:02 pm

    Edit: Now I see the problem better.

    I think that firstly you would have to declare the type as a class parameter:

    class MyClass<Z> {
    

    to get visibility, but now the reason you can’t use it like that is because the static member should be shared among all the instances of the class. But since you could create instances with different type parameters, the static member depending on a particular type would not make sense.

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

Sidebar

Related Questions

I'm trying to dynamically create a connection string during compile time: private static string
I'm trying to access a static method in a Utils class I created: public
I am trying to create a utility class that accepts any type of POJO,
I am trying to create a lightweight template class having a static member initialized
I am trying to create a static global array called series. But the number
I am trying to create a very simple UITableView with static cells from within
I am trying to find why the class cant be created as a static?
So I'm just trying to create a generic mapView in android and put a
I am trying to create a required field validation with a customvalidator. However when
I'm trying to write a CLR user-defined function in F#, but CREATE ASSEMBLY gives

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.