I'm creating a Slick2D game. Right now, I'm creating a Video class, which contains inner classes (FrameSize, FPS, FullScreen..). So I had an OOD idea to crate in way, like we call System.out.println(). That means that I will have public Video class and public static instances of his inner clasess, but netbeans IDE throws me a hint with "Exporting non-public type through public API ". So, should I just ignore that and keep doing the way I was doing or it would be great if you could suggest me your idea?
VIDEO
public class Video {
public static FrameSize frameSize;
public static FullScreen fullScreen;
public static FPS fps;
private Video() {}
public static void loadArguments(Scanner loadInput) {
boolean isVideo = false;
String readLine;
while (loadInput.hasNext()) {
readLine = loadInput.next();
if (readLine.equalsIgnoreCase("video")) {
isVideo = true;
break;
}
}
while (isVideo && loadInput.hasNext()) {
readLine = loadInput.next();
if (readLine.equalsIgnoreCase("end")) {
break;
}
String[] line = readLine.split("=");
String key = line[0];
String value = line[1];
switch (key) {
case "width":
frameSize.setWidth(Integer.parseInt(value));
break;
case "height":
frameSize.setHeight(Integer.parseInt(value));
break;
case "fullscreen":
break;
case "fps":
break;
default:
System.err.println("Unknown video key: " + key);
break;
}
}
}
public static void saveArguments(String filePath) {
Scanner saveInput;
try {
saveInput = new Scanner(new File(filePath));
} catch (FileNotFoundException fne) {
System.err.println("Invalid settings-file.");
return;
}
// TO DO: save function
saveInput.close();
}
class FrameSize {
public final int[][] SIZE_VALUES = {
{800, 600},
{1000, 700},
{1200, 800},
{1400, 900}
};
private int index;
private int width, height;
private FrameSize() {}
public void setSize(int width, int height) {
this.width = width;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
@Override
public String toString() {
return this.width + " x " + this.height;
}
}
class FullScreen {
private boolean fullScreen;
private FullScreen() {}
public boolean isFullScreen() {
return fullScreen;
}
public void setFullScreen(boolean fullScreen) {
this.fullScreen = fullScreen;
}
@Override
public String toString() {
return "" + fullScreen;
}
}
class FPS {
private boolean FPS;
private FPS() {}
public boolean isFPS() {
return FPS;
}
public void setFPS(boolean FPS) {
this.FPS = FPS;
}
@Override
public String toString() {
return "" + fps;
}
}
}
AUDIO
public class Audio {
private static Sound sound;
private static Volume volume;
private Audio() {}
public void loadArguments(Scanner loadInput) {
boolean isAudio = false;
String readLine;
while (loadInput.hasNext()) {
readLine = loadInput.next();
if (readLine.equalsIgnoreCase("audio")) {
isAudio = true;
break;
}
}
while (isAudio && loadInput.hasNext()) {
readLine = loadInput.next();
if (readLine.equalsIgnoreCase("end")) {
break;
}
String[] line = readLine.split("=");
String key = line[0];
String value = line[1];
switch (key) {
case "sound":
break;
case "volume":
break;
default:
System.err.println("Unknown audio key: " + key);
break;
}
}
}
public void saveArguments(String filePath) {
Scanner saveInput;
try {
saveInput = new Scanner(new File(filePath));
} catch (FileNotFoundException fne) {
System.err.println("Invalid settings-file.");
return;
}
// TO DO: save function
saveInput.close();
}
class Sound {
private boolean sound;
private Sound() {}
public boolean isSound() {
return sound;
}
public void setSound(boolean sound) {
this.sound = sound;
}
@Override
public String toString() {
return "" + sound;
}
}
class Volume {
private static final double PITCH = 0.1d;
private double volume;
private Volume() {}
public double getVolume() {
return volume;
}
public void setVolume(double volume) {
this.volume = volume;
}
public void increaseVolume() {
if (!isVolumeRange(this.volume)) {
return;
}
this.volume = this.volume + PITCH;
}
public void decreaseVolume() {
if (!isVolumeRange(this.volume)) {
return;
}
this.volume = this.volume - PITCH;
}
public boolean isVolumeRange(double volume) {
return volume >= 0.0 && volume <= 10.0;
}
}
}
Video
class contains a declaration of a public class variable frameSize
of type FrameSize
.
A public
modifier means, that frameSize
variable is visible to all.
package package1;
public class Video {
public static FrameSize frameSize;
}
// private class
class FrameSize {
}
However FrameSize
is a local class - it is visible only to members of the same package. In the above example, only members of package package1
can see that class, and below code compiles fine:
package package1;
public class Test {
void test(){
FrameSize x = Video.frameSize;
}
}
however this code (different package) gives a compilation error:
package package2;
import package1.*;
public class Test {
void test(){
// this line won't compile - FrameSize class is unknown
FrameSize x = Video.frameSize;
// but this line compiles fine - Object class is public
Object y = Video.frameSize;
}
}
NetBeans warns you about this, because most likely it is unintentional error - why do you want to make some field value accessible to all without publishing the type of this field, that in effect prevents them from using that field ?
If you want make the variable accessible only to other classes within the same package, declare it as protected
, not public
.
But if it is an intentional declaration - then ignore the warning and leave it as is.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With