0
public static int do_dispatch(){

        {
            int prior = this.getPriority();
            ThreadCB threadToDispatch=null;
            ThreadCB runningThread=null;
            TaskCB runningTask=null;
            try {
                runningTask = MMU.getPTBR().getTask();
                runningThread = runningTask.getCurrentThread();
            } catch(NullPointerException e) {}

            // If necessary, remove current thread from processor and reschedule it.
            if(runningThread != null)
            {
                // Check if quantum is exceeded
                if (HTimer.get() < 1)
                {
                    //Increment the priority to lower priority value 
                    prior++;
                    this.setPriority(prior);
                }


                //Append to expired
                expired[prior].append(this);

                runningTask.setCurrentThread(null);
                MMU.setPTBR(null);
                runningThread.setStatus(ThreadReady);
                readyQueue.append(runningThread);

Another example:

class Sub {
    static int y;
    public static void foo() {
         this.y = 10;
    }
}

I'm attempting to compile my program but I continue to get errors with the use of the this keyword in this situation. I understand that 'this' represents the object invoking the method and that static methods are not bound to any object.

What would be the correct way to implement 'this'?

Jason C
  • 38,729
  • 14
  • 126
  • 182
  • 2
    You can't "implement" `this`. [It's defined in the language spec](https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.8.3), and it's a compile-time error to use it in a static method. – Andy Turner Feb 27 '17 at 23:28
  • 2
    `this` refers to the current instance of the class, and in a static context there is no instance -- all methods and properties are at the class level (aka - they are shared across _all_ instances of the class). In order to manipulate static properties from the method you have shown in your code, you would prepend it with the class name, such as `Sub.y = 10`. Note that you don't _need_ to do that here because you're not shadowing any existing properties. Ie: `y = 10` should be sufficient. – Ryan J Feb 27 '17 at 23:33
  • 1
    **DON'T VANDALIZE YOUR OWN POSTS.** Your edit made your question incomprehensible. – user207421 Feb 27 '17 at 23:35

4 Answers4

0

What would be the correct way to implement 'this'?

You simply cannot implement that. There is no concept of this in a static method.

I'm attempting to compile my program but I continue to get errors with the use of the this keyword in this situation

how you can overcome this problem?

you can either:

  • Pass a reference of any object to your static method and use that.

or

  • make your fields and methods instance fields/methods.
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
0

You can't "implement" this. It's a keyword defined in the language spec, and it's a compile-time error to use it in a static method.

If you want to refer to an instance of the containing class, you can always pass in an instance as a parameter:

public static int do_dispatch(YourClass that){
  int prior = that.getPriority();
  // ...

But note that 1) you can't call the variable this; 2) that's basically just an instance method, so why make it static (i.e. you can make it a non-static method that you invoke like that.do_dispatch(), rather than do_dispatch(that))?

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
0

You cannot. There is no concept of this in a static method. You must do one of the following:

  • Make the method not be static.
  • Refer only to static members (note that static member variables are shared among all invocations of said static method and elsewhere). This would be the most appropriate option if your code doesn't actually require a specific instance of whatever class defines do_dispatch.
  • Pass an instance of whatever object to your static method and operate on that instead.

Check out the official tutorial for a bit of insight. If none of the above options can be used to give your program correct behavior, you'll have to rethink your fundamental design a bit based on the what you learn here about static classes.

Jason C
  • 38,729
  • 14
  • 126
  • 182
0

You can't, and you don't need to. Just remove this.. y is unambiguous in the context.

user207421
  • 305,947
  • 44
  • 307
  • 483