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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:57:12+00:00 2026-05-24T05:57:12+00:00

Does the this prefix for accessing member variables exist in Java? Here is my

  • 0

Does the this prefix for accessing member variables exist in Java?

Here is my HelloWorld code:

public class HelloWorld {

    public static int x = 0;

    public static void main(String[] args) {

        HelloWorld.x = 45;

        System.out.println(HelloWorld.x);
    }
}

The above code works with/without the class name prefixing the variable x. However, if i try: this.x = 45; or this->x = 45; I receive an error:

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

I understand member variables can be accessed without the HelloWorld (class name) prefix, like I have done. But, I want to know if the this prefix exists in Java, how do I use it?

EDIT:

Also, could you provide an example where this is appropriate?

duffymo & byte – I greatly appreciate your help. Thanks.

  • 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-05-24T05:57:13+00:00Added an answer on May 24, 2026 at 5:57 am

    You’re attempting to use ‘this’ to refer to a static, not an instance variable. ‘this’ is only used to refer to the instance variables of an instantiated object of this class. You cannot use ‘this’ to refer to static variables on a class.

    When you use ‘this’ you are saying “I want to refer to the variables of this particular instantiation of this class”. A static on the other hand is always going to be the same variable for the class, irrespective of instantiation.

    In addition the correct syntax for referring to an instance variable is by the dot operator:

    this.x = 42; //correct
    this->x = 42; //will not compile as its not valid Java
    

    So essentially what you’re after is something like the following:

    public class Foo {
    
        private int x;
    
        public void setX(int x) {
            this.x = x;
        }
    
        public int getX() {
            return this.x;
        }
    }
    
    public class HelloWorld {
    
        public static void main(String[] args)
        {
            Foo foo = new Foo();
            foo.setX(45);
    
            System.out.println(foo.getX());
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why does this test program result in a java.lang.IllegalMonitorStateException ? public class test {
Does this code cause a memory leak: int main(){ int * a = new
No doubt, it's essential for understanding code to give member variables a prefix so
When accessing a member of some class, I can use e.g.: this->myVar = 10
I cannot understand why this piece of code does not compile: namespace A {
This allows me to get easyly dynamic input variables instead of putting a static
Possible Duplicate: Underscore prefix on property name? What does this mean? @synthesize window=_window; I
Possible Duplicate: Synthesized property and variable with underscore prefix: what does this mean? I've
Possible Duplicate: Synthesized property and variable with underscore prefix: what does this mean? The
Does this pattern: setTimeout(function(){ // do stuff }, 0); Actually return control to the

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.