Jump to content

Some Java questions


lrdblk

Recommended Posts

I have a few questions about Java.

 

  1.  When should I declare a method as "static"? I tried looking it up but I don't quite understand
  2.  What does it mean when Intellij tells me I am making an "unchecked call"?
  3.  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!

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

  1.  When should I declare a method as "static"? I tried looking it up but I don't quite understand
  2.  What does it mean when Intellij tells me I am making an "unchecked call"?
  3.  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 by BoysNoize
Link to comment
Share on other sites

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 by Satire
Link to comment
Share on other sites

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 by Vilius
Link to comment
Share on other sites

I have a few questions about Java.

 

  1.  When should I declare a method as "static"? I tried looking it up but I don't quite understand
  2.  What does it mean when Intellij tells me I am making an "unchecked call"?
  3.  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

  • Like 1
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 by Explv
  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...