简体   繁体   中英

java classes not communicating?

I'm working on a really simple text RPG kind of game for a class. I feel like I get everything fine, but when I run the following classes, I get a compiler error.

This is my "Room" class:

import java.io.*;
import java.util.*;

public class Room {

    public static int size;

    public static void Room(int n) {
        size = n;
    }

    public static void showSize() {
        System.out.println(size);
    }
}

This is the class that calls it:

import java.io.*;
import java.util.*;

public class Dungeon {
    public static void main(String [] args) {
        int mySize = 10;
        Room a = new Room(mySize);

        a.showSize();
    }
}

The weird part is, if I run it without any parameters in the Room() constructor, it's fine, but when I try to pass in a size (either in a variable or explicitly with an int), I get this:

Dungeon.java:8: cannot find symbol
symbol  : constructor Room(int)
location: class Room
        Room a = new Room(mySize);
                 ^
1 error
public static void Room(int n){
    size = n;
}

is not a constructor, it is static method. so, when you try

Room a = new Room(mySize);

Java looks for constructor with parameter and showing compile time error.

Change it to:

public Room(int n){
        size = n;
    }

Read more about constructors here .

You're trying to call a Room constructor, but you haven't defined any. You've got this:

public static void Room(int n){
    size = n;
}

... but that's just a static void method, not a constructor - and it sets a static field. I suspect you really want your Room class to look like this:

public class Room {
    private final int size;

    public Room(int size) {
        this.size = size;
    }

    public void showSize() {
        System.out.println(size);
    }
}

Note that I've made size both private and final . If you really want your rooms to be able to change size after construction then you could remove the final , but I'd strongly advise you to keep fields private .

This is not how you declare a constructor:

public static void Room(int n){
    size = n;
}

Use this instead:

public Room(int n){
    size = n;
}

and makes size non static.

Explanation:

You have actually declared static methods and variables which are not specific to an instance , but belongs to the class . You want to create instances of Room, so the constructor and the members that have to be specific for each instance must not be static.

The reason why it compiles correctly if you run it without any parameter is that if you don't specify any constructor, as you did here, the compiler provides a default parameterless constructor.

For more information on class and instance members, look at this page of the official tutorial .

The problem is that constructor cannot be void and static type so change:

public static void Room(int n){
    size = n;
}

to

public Room(int n){
    size = n;
}

and it will work.

your public static void Room(int n){ isn't a constructor change it to public Room(int n){

you might also want to change the size field to non-static

and you can test that in your main with System.out.println(new Room().size);

public static void Room(int n) {
        size = n;
    }

This seems to be you are trying to create constructor but it is not the way to create.

Make your Constructor and size variable non-static.

public class Room{    
   private int size;
   public Room(int n){
      size = n;
   }
}

Static size will break the encapsulation of Room Object and it should be private.

Room a = new Room(mySize);

Reference

构造函数不返回任何东西,所以将p public static void Room(int n)更改为Room(int n)public Room(int n)并记住你不能沿构造函数放置返回类型,如int,String,void,double, byte,short,Graphics2D等

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM