Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am trying to store input from one class into an array that is in a separate class method, but I'm not really sure how to do this.This is the class that is accepting the input that I want to store into the class method
This is the class my class method.
You should create your array as a static variable rather than a local variable.
public class State
{
private static String[] info = new String[5];
public static void store(State state) {
info[0] = ...
}
...
}
Another better option is to create State as Concrete class rather than a Static class, but you should crawl before you walk :)
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Currently working on a tui application to prompt the user to enter a file name and then I need to get that name from the TUI class over to a model class. Every thing I try gives me the error "Cannot make a static reference to the non-static method getFileName() from the type TUI." Any suggestions on how I should go about the code? I know I haven't supplied a ton of information but I'm not completely sure what information is pertinent.
The problem is that your method getFileName() is not static, so it belongs to an object and you're trying to access it though the class and not an object. If it's the same fileName for every TUI object you just need to make the field fileName and the method getFileName() static.
public static void getFileName() {
If not you have to call the method on an object with the dot operator
TUI tui= new TUI();
TUI.getFileName();
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
Okay let's say I have a Class Game. I instantiate Game as game. Now game contains an object, startupState. How do I change an int in game from startupState?
Sorry if i worded this poorly... code example:
public class game{
int state;
StartupState startupState;
public static void main(String[] args){
Game game = new Game();
}
public Game(){
state = 0;
startupState = new StartupState();
}
}
So with this code how would I change state from the startupState object?
You can't change a Game object from StartupState. The latter is an instance of a separate class, which may well exist without an instance of Game. You can, however, go the other way round, and change startupState from game (unless it's declared final). To change state, you will either need a setter (to access it from outside the current package) or you need to declare it public. Then, you can do
game.setState() // with setter
or
game.state = some_int // if it's public
I hope I interpreted this question correctly. So I assume that an instance of StartupState has a initialState member variable, maybe with a public int getInitialState() {...} instance method that returns it. In that case... theres no really straightforward way to change the state member variable of a Game instance from within the StartupState class (I am assuming thats what you want to do here). To do that, you must call getInitialState() from a instance method of the Game class and set this.state to the return value from the getInitialState() call.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have to create a class. Instance of this class cannot be made. How can I achieve that?
Declare it abstract and add a private constructor.
Do you mean no instances can ever be made? or just one instance?
If no instances can ever be made, then make the class final with a private constructor. All methods then need to be static. A good example of this is java.lang.Math
If you need only a single instance and want no one else to make new instances, then consider using an enum as described in Effective Java 2nd ed:
public enum MyClass{
INSTANCE;
...methods
}
Code that uses this class then invokes methods like this: MyClass.INSTANCE.foo().
If you don't want to instantiate that class then I assume that you only want it to have some static methods. In that case you can easily do it with enum with no constants like
enum MyUtilities{
;//if you want you can place instances of that class here, or not place any
//your methods
public static void myMethod(){
System.out.println("hello");
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
i saw a different signature of main() i.e.
public class Demo {
public static final void main(final String[] args) {
.............
.............
}
}
What is the benefit to make main()'s argument as final. please explain!
Method parameters in Java are local variables. Declaring them as final allows you to access them from inner anonymous classes. Other than that, it can help prevent potential bugs that happen when you change parameter values.
So in your example code, using final String[] args will work just fine.
String[] args is just a variable declaration, it corresponds to the command line parameters, so it can be final.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
myClass1:
public class myClass1
{
public myClass2 myclass2;
public void createsecondclass (String[] args)
{
myclass2 = new myClass2(this);
myclass2.dosomething();
}
myClass2:
public class myClass2
{
public myclass1;
public myClass2(myClass1 myclass1)
{
this.myclass1 = myclass1;
}
public void dosomething()
{
myclass1.another_object_that_could_be_placed_here.dosomething();
}
}
would this not make the code cleaner when trying to access a large amount of objects which all in one way or another are all instantiated under a single class? i ask because i am trying to learn libGDX and in the large assortment of class files that are made to handle each element of the game it just seems easier to pass my application listener down the line since the application listener contains the screen which contains the gameworld which contains the player and so on...
But the problem that i worry about is that by setting it to a variable in the object i am creating a myclass1 that contains a myclass2 that contains a myclass1 and so on. i worry that this might cause memory leaks and since my target is android, memory is a big concern.
if anyone has any thoughts on the subject, directly relevent or not i would appreciate the input. i am after all still learning.
thanks =)