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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:21:57+00:00 2026-06-11T12:21:57+00:00

Here is the whole code : import java.util.ArrayList; public class Test<E extends Comparable<E>>{ ThreadLocal<ArrayList<E>>arraylist=new

  • 0

Here is the whole code :

import java.util.ArrayList;

    public class Test<E extends Comparable<E>>{

    ThreadLocal<ArrayList<E>>arraylist=new ThreadLocal<ArrayList<E>>(){
        @Override
        protected ArrayList<E> initialValue() {
            // TODO Auto-generated method stub
            //return super.initialValue();
            ArrayList<E>arraylist=new ArrayList<E>();
            for(int i=0;i<=20;i++)
            arraylist.add((E) new Integer(i));
            return arraylist;
        }
    };


    class MyRunnable implements Runnable{

        private Test mytest;

        public MyRunnable(Test test){
            mytest=test;
            // TODO Auto-generated constructor stub
        }
        @Override
        public void run() {
                System.out.println("before"+mytest.arraylist.toString());
                ArrayList<E>myarraylist=(ArrayList<E>) mytest.arraylist.get();
                myarraylist.add((E) new Double(Math.random()));
                mytest.arraylist.set(myarraylist);
                System.out.println("after"+mytest.arraylist.toString());
            }

            // TODO Auto-generated method stub

        }
    public static void main(String[] args){

        Test test=new Test<Double>();

        System.out.println(test.arraylist.toString());

        new Thread(new MyRunnable(test)).start();

        new Thread(new MyRunnable(test)).start();

        System.out.println(arraylist.toString());

    }

}

my questions are:

  1. Why new Thread(new MyRunnable(test)).start(); cause the error:
    Cannot make a static reference to the non-static type MyRunnable?
  2. What does the term “static reference” refer to?
  • 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-11T12:21:59+00:00Added an answer on June 11, 2026 at 12:21 pm

    Question 1: Why new Thread(new MyRunnable(test)).start(); cause the error:
    Cannot make a static reference to the non-static type MyRunnable?

    Answer 1: Because you can not instantiate inner class in static context directly.main method is always static.
    To Avoid this error you can use outer class reference to initialize inner class which binds that inner class to specified reference.

     new Thread(test.new MyRunnable(test)).start();//Use test object to create new
    

    Question 2: What does the term “static reference” refer to?

    Answer 2: new MyRunnable(test) is not static you will have to make MyRunnable static in order to access like this.

    You are using generics in most inefficient way 🙂

    Declare MyRunnable<E> Because Generics in static context is different than in normal object context

    If I understand you correctly you want MyRunnable class to understand E that is passed to Test class.
    This is not possible like you are doing.

    You will have to make MyRunnable to understand E and then you can access it.

    static class MyRunnable<E extends Comparable<E>> implements Runnable {
    
        private Test<E> mytest;
    
        public MyRunnable(Test<E> test) {
            mytest = test;
            // TODO Auto-generated constructor stub
        }
    
        @Override
        public void run() {
            Test<Double> test = new Test<Double>();
            ArrayList<Double> doubles = test.arraylist.get();
            doubles.add(new Double(Math.random()));//No type cast needed
        }
        // TODO Auto-generated method stub
    
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's my code: import java.util.Scanner; public class Arrays { public static void main(String[] args)
Consider this code: import java.util.regex.*; public class Pattern3 { /** * @param args the
i've a jquery (UI) App here where the whole JQuery Code gets a little
Here's the whole page: <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Strict//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd> <html> <head>
The following code uses the concept of method overriding in Java. package pkg; import
<?php while(true){ //code goes here..... } ?> I want to make a PHP web
I have a problem using JSON and arrays. Here is my code: while($row =
I here by post the code why I came across while exploring one technique.
Here's my code for reading my db data: OdbcDataReader rdr = cmd.ExecuteReader(); while (rdr.Read())
Here is some simple code: DIR* pd = opendir(xxxx); struct dirent *cur; while (cur

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.