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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:00:49+00:00 2026-06-04T17:00:49+00:00

Here is a simple example of some code that compiles using Java 6, but

  • 0

Here is a simple example of some code that compiles using Java 6, but does not compile in Java 7.

public class Test<T extends Test> {

  private final int _myVar;

  public Test(int myVar) {
    _myVar = myVar;
  }

  public int get(TestContainer<T> container){
    T t = container.get();
    return t._myVar;
  }

  private static class TestContainer<T extends Test> {
    private final T _test;
    private TestContainer(T test) {
      _test = test;
    }
    public T get(){
      return _test;
    }
  }
}

In Java 7, it fails to compile in the get(TestContainer<T> container) method, with the error:

error: _myVar has private access in Test

I don’t understand why this no longer compiles – in my mind it should. The variable t is of type T, which must extend Test. It’s trying to access the field _myVar of a instance of Test from within the class Test.

Indeed, if I change the method get(TestContainer<T> container) to the following, it compiles (with no warnings):

public int get(TestContainer<T> container){
  Test t = container.get();
  return t._myVar;
}
  • Why does this no longer compile?
  • Was this a bug in Java 6? If so why?
  • Is this a bug in Java 7?

I’ve had a google and searched in the Oracle bug database, but haven’t found anything on this…

  • 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-04T17:00:50+00:00Added an answer on June 4, 2026 at 5:00 pm

    §4.9 … Then the intersection type has the same members as a class type (§8) with an empty body, direct superclass Ck and direct superinterfaces T1′, …, Tn’, declared in the same package in which the intersection type appears.

    From my understanding of that JLS part, your case with a type variable <T extends Test> creates the following intersection:

    package <the same as of Test>;
    
    class I extends Test {}
    

    Therefore when you access members of the type T you actually access members of the intersection type I. Since private members are never inherited by subtypes accessing such member fails with compile-error. On the other hand access to package-private (default) and protected members is allowed by the fact the intersection is

    … declared in the same package in which the intersection type appears.

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

Sidebar

Related Questions

This problem is not readily reproducible in a simple example here but was wondering
Here is some sample code that illustrates the problem. It compiles and runs correctly,
I'm using simple-html-dom to try and pull some code from another site. Here's some
I can't get past this issue I am having. Here's a simple example: class
I am learning and trying simple example using node.js and mongoskin. here is my
I have a simple java project (adapted from the example here ), which is
Here's an interesting problem. Is there a way to write some code using LINQ
Help Perlers! Does anyone know a simple insert code here approach would bring code
I'm trying to write some code that will render a texture onto a simple
I have some simple code that is comparing two float values to illustrate a

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.