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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T12:37:26+00:00 2026-06-02T12:37:26+00:00

I have a very simple class which I want to use as a subclass

  • 0

I have a very simple class which I want to use as a subclass of another one. But when I put its code in the parent’s class I get :

non-static variable this cannot be referenced from a static context

On the other hand when I put the sublass GenTest‘s class code outside the the “parent’s” class code – JavaApp1 I do not get this error.

public class JavaApp1 {

    class GenTest {  
        @Deprecated
        void oldFunction() {
            System.out.println("don't use that");
        }
        void newFunction() {
            System.out.println("That's ok.");
        }
    }

    public static void main(String[] args) {
        GenTest x = new GenTest();
        x.oldFunction();
        x.newFunction();
    }
}

Why is this happening ?

  • 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-02T12:37:32+00:00Added an answer on June 2, 2026 at 12:37 pm

    Your nested class (which isn’t a subclass, by the way) isn’t marked as being static, therefore it’s an inner class which requires an instance of the encoding class (JavaApp1) in order to construct it.

    Options:

    • Make the nested class static
    • Make it not an inner class (i.e. not within JavaApp1 at all)
    • Create an instance of JavaApp1 as the “enclosing instance”:

      GenTest x = new JavaApp1().new GenTest();
      

    Personally I’d go with the second approach – nested classes in Java have a few oddities around them, so I’d use top-level classes unless you have a good reason to make it nested. (The final option is particularly messy, IMO.)

    See section 8.1.3 of the JLS for more information about inner classes.

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

Sidebar

Related Questions

So I have a very simple class that has a method called getThumbUrl() but
I have a very simple scenario, using NHibernate: one abstract base class animal; two
I have a very simple code: package mygame; public class RunGame { public static
I have this very simple C++ class: class Tree { public: Node *head; };
Let's say I have a very simple PrototypeJS class that looks like this: var
i'm missing something fundamental here. i have a very simple custom class that draws
I have a class that uses org.apache.ant and the class is very simple, it's
Very simply put: I have a class that consists mostly of static public members,
I have a very simple problem. I have a player on my website which
I have a very simple problem. I have an application which is written in

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.