I have the following code:
public class main {
public static void main(String[] args) {
Thready howdy = new Thready();
Thread howYouDo = new Thread(howdy);
howYouDo.start();
}
}
class Thready implements Runnable{
int hi = 1;
public Thready(){}
public void run() {
for(int i = 0; i < 10; i++) {
System.out.println(++hi);
}
}
}
It runs fine in IntelliJ regardless of the #Override annotation. In Eclipse, it forces you to remove the #Override annotation, and it works. In NetBeans, it warns you to add an #Override annotation, and the code runs, but the THREAD IS NEVER STARTED in NetBeans no matter what. Meaning the console does not print out anything.
What is going on?
Related
I wonder if there is a way (a gradle script or any script or any other way without an IDE) to remove methods annotated with certain annotations. Example:
class x {
public static void main(String[] args) {
int x = getValue();
System.out.println(x);
}
#RemoveEnabled(id = "getValueMethod1", return = "10")
int getValue() {
return 20;
}
}
Now when I run the script or gradle target, it should remove the getValue() method, and the output code should become:
class x {
public static void main(String[] args) {
int x = 10;
System.out.println(x);
}
}
Is there an existing script or way to achieve this? It might be achievable with grep and String parsing etc., but I'm looking for a cleaner solution which is able to get all methods by an annotation id, and remove them with formatting. I tried searching on Google, Stack Overflow etc., but couldn't find a solution.
I wrote a module to process similiar task.
https://github.com/KnIfER/Metaline
#StripMethods(keys={"ToRemove","AlsoRemove"})
public class YourCLZ{
void ToRemove(){} // will be removed
void AlsoRemove(){} // will be removed
#StripMethods(strip=true)
void AnotherTest(){} // will also be removed
}
in your case
#StripMethods(keys="getValue")
class yourx {
public static void main(String[] args) {
int x = getValue();
System.out.println(x);
}
int getValue() {
return 20;
}
}
will become:
class yourx {
public static void main(String[] args) {
System.out.println(x);
}
}
you will get a compilation error since for now my module simply remove any methods or method body statements that contain the key you specified.
I am trying to create a system where one variable can be changed from different classes. The code that I am trying to create is this:
Server:
public class Server(){
public int number = 0;
}
Client:
public class Client extends Server(){
public void run (){
number++;
}
}
Business:
public class Business(){
public void run(){
number++;
}
}
I have another class which instantiates the Client and Business class. The result when I print out the number from a different class is still 0, I am trying to understand the concept of how could the same variable be manipulated from different classes. Do I have to even extend classes, is there some other approach?
This is because in your case the number variable is an instance variable, which means that each instance has its own copy of this primitive.
You will get more understanding by reading about instance and class variables.
So if you will make your number variable as a class variable by adding static keyword - your application should start to work (provided that you fix compilation errors).
I also recommend you to read about AtomicInteger which would be more safety in multithreading.
P.S. After rewriting of your code I get following code:
class Test {
public static void main(String... args) {
Client c = new Client();
c.run();
Business s = new Business();
s.run();
Server ss = new Server();
System.out.println(ss.number);
}
}
class Server {
public static int number = 0;
}
class Client extends Server {
public void run() {
number++;
}
}
class Business extends Server {
public void run() {
number++;
}
}
You have a simple methods to achieve this.
1) Use number as a Static variable and access the method. ( Use Synchronized method to increment the value for multi threaded application)
2) Use AtomicInteger to increment. ( No need Synchronization)
(https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicInteger.html)
If you need more explanation please let me know.
You can use the solution above mentioned by #Mikita Berazouski.
Or you can use Static Method. Like this you do not need to extend Server.
Static methods are the methods in Java that can be called without creating an object of class. They are referenced by the class name itself or reference to the Object of that class.
Your solution will be like this :
public class Test {
public static void main(String... args) {
Client c = new Client();
c.run();
Business s = new Business();
s.run();
System.out.println(Server.number);
}
}
class Server {
public static int number = 0;
public static void run ()
{
number ++;
}
}
class Client {
public void run() {
Server.run();
}
}
class Business {
public void run() {
Server.run();
}
}
try the code above here and you will check the result.
Another possible solution since you declare number as static in class Server is to write code like this, this allow you to do diff thing in the void run of each class :
public class Test {
public static void main(String... args) {
Client c = new Client();
c.run();
Business s = new Business();
s.run();
System.out.println(Server.number);
}
}
class Server {
public static int number = 0;
}
class Client {
public void run() {
Server.number ++;
}
}
class Business {
public void run() {
Server.number ++;
}
}
I am trying to do following in gui class for notifying registred observers.
public class GUI extends javax.swing.JFrame implements Observer {
public notImportantMethod() {
t = new Thread() {
#Override
public void run() {
for (int i = 1; i <= 10; i++) {
myObject.registerObserver(this);
}
}
};
t.start();
}
}
It gives me error: incompatible types: cannot be converted to Observer How can I use this? I know inside of run there is another context but how could I access it?
this now refers a Thread. You should be able to call GUI.this. For more info, see here .
If you're looking for quick and dirty: (this is not good practice)
public notImportantMethod() {
final GUI self = this;
t = new Thread() {
#Override
public void run() {
for (int i = 1; i <= 10; i++) {
myObject.registerObserver(self);
}
}
};
t.start();
}
}
Otherwise I would recommend looking up a tutorial on multi-threading and/or concurrency in java, like this one: http://winterbe.com/posts/2015/04/30/java8-concurrency-tutorial-synchronized-locks-examples/
#Ishnark has answered it correctly. You should be able to access it via GUI.this, that's all that you need to do.
I have a main class in which I am calling java method with #Asynchronous annotation on it. I have imported javaeeAPI6.jar for it but the method runs synchronously. Please tell me what is not right. I have added a long for loop to check whether the code goes into next statement past the asynch asyncrounousCall() call but it hangs on it
import javax.ejb.Asynchronous;
public class TestClass
{
public static void main (String[] args)
{
System.out.println("synch");
createAsyncrounousDocument("", "");
System.out.println("synch");
}
#Asynchronous
public static void asyncrounousCall()
{
for(int i = 0; i < 9999999999L ; i++)
i = i;
System.out.println("asynch");
}
}
Thank you
Aiden
I am just trying to create an example program to help me remember how to operate for loops, when I ran it through the compiler. The Compiler said missing return statement. Where do I add it?
Here is the code:
public class LoopExample {
public String bam() {
for (int i = 0; i < 8; i++) {
System.out.println(i);
}
}
}
EDIT
I received an answer, but now the main says 'cannot find symbol'... here is the code for the main:
public class LoopExampleTestDrive {
public static void main(String[] args) {
bam looper = new bam();
System.out.println(looper);
}
}
I would advise you to try to understand how Object Oriented languages work first.
That being said, the main reason why your code doesn't work, is because you try to make an object of the class bam with new bam(). This class unfortunately doesn't exist as it is only a method in a class. My solution would look like:
public class LoopExample {
public void bam() {
for (int i = 0; i < 8; i++) {
System.out.println(i);
}
}
public static void main(String[] args) {
new LoopExample().bam();
}
}
As I said: try to understand object oriented programming first, before trying to continue programming in Java. It is too essential to be able to write working code.
PS: just to be complete, the best way to write what you want to do, would look as follows.
public class LoopExample {
public static void main(String[] args) {
for(int i = 0; i < 8; i++) {
System.out.println(i);
}
}
}
Change the signature of your method. Replace public String bam() with public void bam(). voidmeans that you return nothing instead of a String as before.
For further information see http://www.tutorialspoint.com/java/java_methods.htm