intellij println declaration [closed] - java

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 2 years ago.
Improve this question
I am trying to use ''sout'' keyword in intellij for calling system.out.println() quickly but it seems like doesnt work.
Looks like, I have some problems in settings sections.
Does anyone know the way the quick way problem
It Looks like this:
I solved the problem it was all about defining println() inside a method.
I solved it. The solution is that declaring the println() method within class or
inside another method as well.
class Main {
public static void main(String args[]) {
System.out.println(fun());
}
int fun()
{
return 20;
}
}

Yeah just use it inside in any method, like this:

According to Intellij support question
Make sure you are inside the method where this completion makes sense, not just inside the class.

Related

How do I test if i got notified [closed]

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 12 months ago.
Improve this question
I have a class with a method which somtimes uses notifyAll().
class A{
public void method(*some args*) //sometime i call for notifyAll()
}
I want to build a test to see if class A has called for notifyAll()
#Test
public void test(){
A.method
boolean isNotified
}
how can I do it?
Couple of ways
Verify it has been called by checking the effects of potential call - eg some object did change its state as a result and it is verifiable
Spy on instance that is supposed to have method called - you can then check if the method have been called directly
Use observable pattern and add 'observe' behavior to the test case.
probably much more.

Java class convention dealing with arraylist [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
My question is in regards to better convention for class design. I am currently working on a project for school where I am to use a class to create an object for a unit conversion calculator. I want to store these objects into an arraylist.
My question is, does it matter if I create and .add to the arraylist from the main method, or is it better to think about using the conversion class to deal with the array?
The main method is a starting point for execution. That's a place only for processing application arguments (args) and creating an instance that represents the whole application*. Normally, the class that contains main should be stateless.
A good example - a Spring Boot initialiser:
public class Runner {
public static void main(String... args) {
SpringApplication.run(Runner.class, args); // all the magic is in there
}
}
*
Sometimes, to adhere to the single responsibility principle, we direct args handling and initialization of the principal object to different classes.
It is better to create a separate class. It is very good practice and help you a lot to maintain when your project will grow in future.

test a java method in netbeans [closed]

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
Let's say I found this piece of code and I want to test it in Netbeans before I incorporate it:
ArrayList list;
for(String s: list)
{
Integer c = stringsCount.get(s);
if(c == null) c = new Integer(0);
c++;
stringsCount.put(s,c);
}
Is there a way to test the above code in Netbeans without having to create a temp class with a main? I believe there is a way in Eclipse but I am looking for a way in Netbeans. Thank you.
Is there a way to test the above code in Netbeans without having to create a temp class with a main?
As far as I know, you can't do that. Your program needs an entry point. The main method is supposed to be your program's entry point.
If you are reluctant to always create a new class. You can always have a class for testing. Just replace the old codes with new testing codes whenever you need it.
P.S: I don't think it can be done in Eclipse as well.

For loop Testing in java [closed]

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 7 years ago.
Improve this question
public void numberOfChoices() {
for (int i = 0; i < 10; i++) {
int guess = scan.nextInt();
}
}
how can i test this for loop in junit. my method should be void.
i think i couldn't make myself clear. my question is that how can i test the loop that actually it goes ten time? i hope you understand now. but i don't know some people just try to be oversmart and give negative point to my question i think if you couldn't understand ask for more explanation. and this is question is not that simple to solve too so don't understand why i got negative....
Writing code easily to test is the first step for writing test.
Every single method should have their meaning and only do one thing.
If you face problem in writing test, you might think about it.
If your method must be void and you don't work with database I would advise to create getters for the variables that you want to verify. For the loop you might want to have some Array or List, for example, and then in your test do something like this:
assertEquals(expectedList, yourClass.getList);
However, it is not the ideal scenario because you should not change the production code to fit your testing needs. If you work with database then create a separate query in the test to fetch the needed data.

Synchronization on Java Method [closed]

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
I have a public class called ProcessOrder. Within this class I have a private method called Login(), which logs into another company's system. I want to be able to initiated as many ProcessOrder objects as I want, but the Login() process to be synchronized, which means all the ProcessOrder objects need to wait inline to process Login().
What's the best practice to design my code?
Thanks,
You could use a synchronized block, specifying the object on which to synchronize on (here, the ProcessOrder class):
public class ProcessOrder {
private void login() {
synchronized(ProcessOrder.class) {
// one at a time, please
}
}
}

Categories