I'm having trouble creating the most simple of methods in eclipse, I keep getting an error on the line that the method header is declared it looks like This(error information is in the console) This is just the code that i have to have written for the error to pop-up.) I haven't written code since before winter break so I don't know if I may have messed up my jdk or jre, but in all my past projects the methods work as they should and there are no errors, even if I create a new method.
edit: thanks dimoniy, it's been a long winter.
You cannot declare methods within other methods. Just move your method out of the main()
Related
I just started using IntelliJ and while creating my first class and method I noticed something. When I wrote GomokuClient(4000); to call on a class from a imported library I of course got a error for the code not being complete, so I pressed the little red bulb to see the issue, and I selected the recommended fix. The fix looked like this:
Now I'm wondering, how do I write the portNumber:label in the parameter myself for other methods. Looked really nice and very helpful, and I'd like to know how to do this myself.
thanks beforehand!
The fix done by the IDE was not about setting parameter label, but instead adding the new() for ensuring the constructor call.
What you additionally see there is a feature of IntelliJ to show method's parameter hints/labels for easy understanding (readability) of the code. You can read more about parameter hints here: https://www.jetbrains.com/help/webstorm/viewing-method-parameter-information.html.
I am currently using Eclipse to code Java with. Recently my lecturer noticed that my print statements in my Java is weird and he does not know a fix to it. It shows System.outprintln() instead of System.out.println(). When I change it to System.out.println() it is underlined with a red line.
Is there a quick fix? He says that I might get marks deducted if I submit my codes in this format without changing it.
Try to use full named class:
java.lang.System.out.println("some value");
If this case would work correctly - you have some mistake with class naming (possibly in manner like in Andy Turner's comment)
PS It would be much easier to assume the reason of problem if you'll provide sketches of your sources.
out is static data member of System class having type of PrintStream.
println() is overloaded method of PrintStream class.
System.outprintln() is compiler error.
Try to compile the class using command prompt using the following command- javac myclass.java and then run it using java myclass.
System.outprintln() should throw a compiler error.
Please note that any IDE is user friendly and helps to write your code faster by providing suggestions but your assignments should not be based completely on them. :)
In intellij IDEA, if a method is unused, the method is shown in a gray color. But in some cases, IDEA doesn't grey out the method, but when I check the references of those methods using alt + F7, IDEA says that the method is unused.
Is this a IDEA bug or is there any reason why IDEA wouldn't grey out these specific methods? If it is a bug, is there some workaround to make IDEA identify that method is unused?
Most likely it's not a bug, it's a limitation for performance reasons. Methods likely to take a long time when searching for usages are skipped.
A workaround is to run Unused Declaration inspection explicitly in all your project via Analyze | Inspect Code or Analyze | Run Inspection by Name. That'll take some time. You can also set up TeamCity server to do it for you automatically every night.
I used to have it working like charm, but one time, I by mistake clicked on alt+Enter on an unused method, and chose to suppress the inspection on unused code. Ever since then, I stopped getting the grayed out methods and code, so since there is a way to get it undone, there sure must be a way to get it back working.
After 5 minutes of searching, I found a solution:
Settings --> Editor --> Inspections --> Java --> Declaration Redundancy --> Unused Declaration
Make sure you check "Unused Declaration"
And I just checked by creating a new useless method, working like a charm.
My answer is quite late, but perhaps it will help others to identify their problem:
IntelliJ didn't mark methods as unused for me, because they were overloaded methods, for example:
1: methodName(String argument)
2: methodName(ArrayList<String> argument)
The first method was no longer used, but the second method was. IntelliJ (I assume) simply checks the method names, and sees that the method name is used, even though one of them is no longer used.
I have checked the other answers on this page for finding unused methods, but have not found a solution to filter out unused, overloaded methods.
It might be a bug if you're using a method with a very common name.
If you tried #Peter Gromov method above, and your method is still yellow, it might be the case that this is a bug.
I had a very common named method, named "stop".
Looking for usages (using ALT + F7) didn't show anything.
Analyzing the whole project, clearly showed that this method did not have any use.
Despite that, the method was still yellow.
I was surprised to find out, that if I try to refactor the method name, I get a pop-up warning that this will change in other places as well.
Turns out, that the refactoring warned about changing the method name in TODO comments. Somehow Lint recognized the TODO comments as using this method.
My advice is to just not name your methods as something that may be written in a TODO comment.
See this image, where I am using a method named "stop" :
I'm trying to work on the CS project where I have to draw a bunch of shapes using a "Turtle" library. However, When I try to call upon the method to be used, it gives me and error. I'm pretty sure it has to do with the method parameters, which I'm just learning about now and still don't usually know which to put in. Any advice?![enter image description here][1]
http://i.stack.imgur.com/Yn32y.png
You don't have to specify the parameter type at the method call, so remove Turtle from drawRectangle(Turtle tParam), it should be like this - drawRectangle(tParam)
The error refers to a problem in your program's code. Specifically you forgot to close a bracket somewhere.
OK, so before we start let me state that I have been googling and searching for an answer to my question for quite a while now and haven't been able to find a suitable one (the keywords are tricky since I keep getting unrelated posts and sites as results).
Now, moving on I have a Java class that contains a my main method and a number of other functions. I want to test these functions using JUnit but I can't instantiate a class that has main in it, if I simply try to call the function I get an error saying the function is outside the namespace even though both files are in the same package, and I get an error trying to import the file.
Is there anyway to test these functions using JUnit?
P.S. Yes I know that you can put them in a new class, but I don't think it is overkill to create a new class just for testing or to put in 2 functions that are for parsing user input, and there is still the issue of testing the main function itself (and it is not uncommon to write a main method just for testing).
So this is what happened. Since I don't use Java very often I ended up creating private data members in the class but treated them as I would globals in a C++ program. In consequence I initialized them in main and didn't think of making a constructor and hence the problem with instantiating the class. When that didn't work I tried the . form but since the methods referenced the private data members I would get an error without instantiating the class. Thanks to the guys that noticed the constructor thing.
You absolutely can create an instance of a class which contains a main method, so long as it has an accessible constructor of course.
Likewise you absolutely can call a static method directly, using MyClassName.myMethodName.
Having a main method in a class makes absolutely no difference to it in terms of the Java language itself - so you can test it just as you would any other class.
Very strange. I just wrote SomeClass with main inside and it's perfectly testable by SomeClassTest class.
Just a thought, did you declare constructors as private in the class with main method? It will help a lot if you can post some code snippet and exact error message you're getting.