lrdblk Posted January 20, 2017 Share Posted January 20, 2017 I have a few questions about Java. When should I declare a method as "static"? I tried looking it up but I don't quite understand What does it mean when Intellij tells me I am making an "unchecked call"? At what point does it make more sense to create a separate class rather than use enum's If you need to see my script to make sense of any of the above questions, let me know and I'll post it Thanks for your help! Quote Link to comment Share on other sites More sharing options...
Team Cape Posted January 20, 2017 Share Posted January 20, 2017 static - means that a method is not inherent to a specific instance of the class e.g. class V { static void x() { } } you would call V.x() instead of new V().x() because it does not need an instance to run the method. think of it as something unrelated to the actual instantiated object, but rather an added functionality to the class itself. unchecked call - not sure, maybe someone that uses intellij can respond make a separate class vs. enums - depends on what is easiest and/or more organized and logical. both have their benefits. Quote Link to comment Share on other sites More sharing options...
BoysNoize Posted January 20, 2017 Share Posted January 20, 2017 (edited) When should I declare a method as "static"? I tried looking it up but I don't quite understand What does it mean when Intellij tells me I am making an "unchecked call"? At what point does it make more sense to create a separate class rather than use enum's 1. A member (Method or data field) should be declared static when you want it to be a member of the class rather than a member of an instance of the class. For example, if we had an object called Ball, it has all of its internal object information (Size, bounciness, etc.). We might also want information such as how many Balls there are. A single ball object doesn't have the scope to see how many other balls there are, so instead, we might have a static counter to hold this information, which we can access through Ball.getCount(). This might help. 2. Might need to see the code to tell, usually you get them when you aren't handling generics correctly, but there might be cases I don't know about. 3. Not entirely sure what you mean by this. I assume you mean splitting functionality into different classes rather than putting all your functionality into a single class. There isn't really a definite point, it's more of a "feel" thing. If you feel like your code is getting difficult to manage, split it up. OO programming tells you that each class should perform a discrete task. of a young Steve Jobs talking about the OO approach, maybe it will help you in understanding when to divy up tasks. Edited January 20, 2017 by BoysNoize Quote Link to comment Share on other sites More sharing options...
Satire Posted January 20, 2017 Share Posted January 20, 2017 (edited) Static just means you are not instantiating that object. Non-static means that you will be instantiating that object.So when you make a static method, it means everything in that method cannot be created. Let's say you have a public class that isn't static. You could go classname varname = new classname(); varname.function. if you make a static method and tried that, it would say you couldn't. So basically static means everything is already there. You are not creating a new instance of that object, Edited January 20, 2017 by Satire Quote Link to comment Share on other sites More sharing options...
Vilius Posted January 20, 2017 Share Posted January 20, 2017 (edited) As I can see noone is answering your second question about unchecked warning Most likely you have an ArrayList<?> list = ... The warning comes from you not using generics in <?>. To fix it you just need to specify the type of the list by doing <String> or <Integer> or whatever you want to use. This answer might not apply to what you are doing because you didnt provide any code to us to look at where the warning happens. Edited January 20, 2017 by Vilius Quote Link to comment Share on other sites More sharing options...
Explv Posted January 20, 2017 Share Posted January 20, 2017 I have a few questions about Java. When should I declare a method as "static"? I tried looking it up but I don't quite understand What does it mean when Intellij tells me I am making an "unchecked call"? At what point does it make more sense to create a separate class rather than use enum's If you need to see my script to make sense of any of the above questions, let me know and I'll post it Thanks for your help! 1. Generally you should try and avoid the use of static unless you are absolutely sure it is correct to use it, and makes sense to use it. A static method belongs to the class and not any instance of the class. It is commonly used for utility methods, another example of it's use would be in the Singleton Pattern. You will find a lot of amateur programmers abusing the static keyword due to their lack of understanding of OOP. 2. You are probably getting this warning because you are using raw types, read a tutorial on Java generics 3. An Enum is a class, it can still have methods etc. It is generally used when you want to specify a fixed list of constants. For example you may have a Tree Enum that specifies a fixed list of Trees someone can choose from in your script. TL;DR Follow some more tutorials and learn some more Java 1 Quote Link to comment Share on other sites More sharing options...
lrdblk Posted January 20, 2017 Author Share Posted January 20, 2017 As I can see noone is answering your second question about unchecked warning Most likely you have an ArrayList<?> list = ... The warning comes from you not using generics in <?>. To fix it you just need to specify the type of the list by doing <String> or <Integer> or whatever you want to use. This answer might not apply to what you are doing because you didnt provide any code to us to look at where the warning happens. Here is the code comboBox.setModel(new DefaultComboBoxModel<>(script.getTrees())); getting the warning right over the comboBox.setModel part Quote Link to comment Share on other sites More sharing options...
Explv Posted January 20, 2017 Share Posted January 20, 2017 (edited) Here is the code comboBox.setModel(new DefaultComboBoxModel<>(script.getTrees())); getting the warning right over the comboBox.setModel part You probably declared your JComboBox like: JComboBox comboBox = new JComboBox(); When it should be: JComboBox<Type> comboBox = new JComboBox<>(); Where "Type" is whatever type script.getTrees() returns Edited January 20, 2017 by Explv 1 Quote Link to comment Share on other sites More sharing options...
lrdblk Posted January 20, 2017 Author Share Posted January 20, 2017 You probably declared your JComboBox like: JComboBox comboBox = new JComboBox(); When it should be: JComboBox<Type> comboBox = new JComboBox<>(); Where "Type" is whatever type script.getTrees() returns Ahhhh that makes sense, thanks! Quote Link to comment Share on other sites More sharing options...