1

Maybe some people have posted something like this before, but many questions are kind of convoluted and hard to understand, and I would like to get at the essence of my issue without going through 3 pages of unrelated code. My issue is this:

When I try to instantiate my class, it tells me it has to be static. I am very confused at this.

package project0;

import javax.swing.*;
import java.util.*;
import java.awt.Button;


public class Project0 {

public static void main(String[] args) {

Test7 lucky7 = new Test7(); //here is where I am instantiating my class and it gives "nonstatic variable this cannot be referenced from a static context." 
}

class Test7{

    Test7(){
        String str1 = "Hello";
        changeString(str1);
        System.out.println(str1);
    }

    public void changeString(String str2){
        str2 = "Goodbye";
        }

}

If I change the test7 class to static it works, but I feel this is the opposite of what should be happening, as I thought static classes couldn't be instantiated.

edit: I guess I was mistaking static for abstract >.> but in that case what is different about a static class than a regular class. Still a bit confused.

Please try to make answers simple so I can understand

Jared Moen
  • 21
  • 1
  • 3
  • 1
    _as I thought static classes couldn't be instantiated_ Why did you think so? – Sotirios Delimanolis Feb 17 '15 at 07:39
  • 2
    You're mixing a *lot* up here. First of all, post the code that you're actually compiling - all in one go. This likely has little to do with a static class and a lot to do with you using the `this` keyword somewhere. – Makoto Feb 17 '15 at 07:40
  • What do you think the problem is, @Makoto? – Sotirios Delimanolis Feb 17 '15 at 07:43
  • If you're going to vote to re-open a question closed a duplicate, please comment first. I'm here to review. I don't just drive by. – Sotirios Delimanolis Feb 17 '15 at 07:44
  • There's a lot we're not being told. At best, `Test7` is an inner class, not a static class, if it's declared inside of another class. It's likely a confusion as to how that class should be instantiated. – Makoto Feb 17 '15 at 07:45
  • _If I change the test7 class to static it works_ The only way they could do that is if `Test7` was declared within whatever class the `main` was declared in. Whether that's the right place to instantiate or not is secondary. The issue is a misunderstanding of nested types. – Sotirios Delimanolis Feb 17 '15 at 07:47
  • To add to the confusion, my next follow up question would be about the meaning of "it works": What's the expected outcome? Should this print "Hello" or "Goodbye"? Independent of the inner class being static. – Olaf Kock Feb 17 '15 at 07:47
  • And [here's](http://stackoverflow.com/questions/70324/java-inner-class-and-static-nested-class) the bigger picture. – Sotirios Delimanolis Feb 17 '15 at 07:51

1 Answers1

1

static classes can definitely be instantiated.

Your main method as well as your Test7 class are inside some class. When Test7 is not a static class, each instance of Test7 must have an enclosing instance, which is an instance of the class that contains Test7. That's why calling Test7 lucky7 = new Test7() from your main method doesn't work in this case.

A static class, on the other hand, doesn't have an enclosing instance, which is why Test7 lucky7 = new Test7() works when you change Test7 to be static.

Eran
  • 387,369
  • 54
  • 702
  • 768