How to make a string in to code [duplicate] - java

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Convert String to code
Run piece of code contained in a String
is there any way to turn a variable(string) into to a piece of code?
My string has to be changeable (non-static)
If you can explain it simply that would be nice :D

I assume, that you want to create a String object with some Java statements and execute the content as if it was a method.
No, it is not possible, because that would imply, that java was an interpreted programming language. Which is not the case.
You could merge the line with some sort of class template (on another string), store it to a file, compile that using the jdk (call javac with `Runtime.exec) and load the class with a custom classloader, then reflect the method and hope for the best.
There is a rather complicated way using the fairly new Java Compiler API, but I guess, that is far beyond your needs. (And there have to be some more complicated tricks, because the eclipse IDE provides views that allow executing Java statements.)

Related

How does setting instance variable public and not using setter method might break collaborators' code who are using my class in java? [duplicate]

This question already has answers here:
Why use getters and setters/accessors?
(37 answers)
Closed 3 years ago.
I found the following passage in the book "Headfirst java". I don't understand the bolded part in the below passage.
Object: The point to setters (and getters, too) is that you can change
your mind later, without breaking anybody else's code! Imagine if half
the people in your com- pany used your class with public instance
variables, and one day you suddenly realized,
" Oops - there's something I didn't plan for with that value, I'm going to have to switch to a setter method." You break everyone's
code. The cool thing about encapsulation is that you get to change
your mind. And nobody gets hurt. The performance gains from using
variables directly is so miniscule and would rarely be worth it.
The internal representation of that value may change and require a new type be used, which could be hidden from other code using getter/setter methods.

Can I call a method in a class annotation in Java? [duplicate]

This question already has answers here:
Which types can be used for Java annotation members?
(4 answers)
Closed 4 years ago.
Can I able to call a method which returns string inside an annotation.
If so please guide me how to achieve this?
I tried like this but this doesn't work for me.
#Description(value = Resource.getWord("key"))
An annotation only takes compile time constants (as they might be used during compile time), therefore you cannot make any calculation within the definition, as they are unknown during the compile time.
Allowed constant types are (taken from java-annotation-members):
Primitive
String
Class
Enum
Another Annotation
An array of any of the above
Possible solution for your situation:
As I understand you would like to localize the #Description content.
As this is only meant to be exposed to other developers anyway, you are safe to simply use English, in my opinion. Localization is for the end user, not the developer.
I can imagine an aspect being wired up to process methods annotated like this, where the "key" is in the annotation, and the aspect processing then uses the key at run time... but I'm not sure this is what you're looking for.

Static and Final Rules of Interface [duplicate]

This question already has answers here:
Is "public static final" redundant for a constant in a Java interface?
(6 answers)
Closed 3 years ago.
What exactly the meaning of instance fields in JAVA ?
As per am knowing in JAVA :
An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final.
But, When I have tried as below :
interface TempIn
{
TakeInput tv=null;
String name="";
int temp=0;
void printT();
}
and it's working. How ?
Confused...
Simple: all these fields are static and final by default.
Therefore the java language allows you to write down something that is implicitly given.
In other words: imagine the "compiler" putting down the keywords for you.
But I agree, this is a bit of confusing. And it also turns a bit into a "style" thing. In the early years of Java, a lot of people would add these redundant keywords to their interfaces. On the other hand, "clean code" tells us to avoid redundancy in our code. And nowadays, an IDE like IntelliJ will even give you warnings when using the keywords. So, my recommendation:
don't touch old, existing code
talk to your team, and decide what makes sense for you, and for new code, follow that agreement

why are access modifies used so much in Java but not in C#? [duplicate]

This question already has answers here:
Access modifier best practice in C# vs Java
(4 answers)
Closed 5 years ago.
Namely, why is the Java main method public but the C# Main method is defaulted to internal? I also find that classes in C# often don't need to denote an access modifier but in Java we slave over ensuring visibility is restricted just to those that need to see the information of these other classes.
In dotnet the Mainmethod doesn't have to be public. It can be private inside a private class and the runtime will still be able to resolve it. You may want to change the modifiers on Main for your own reasons though.
You can find more detailed information here https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/main-and-command-args/
In general though dotnet developers do (or should) give the same amount of consideration to access modifiers as Java developers.

An use for Java's #override [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What’s “#Override” there for in java?
Since Java 1.5 this annotation was incorporated to the language to be used on methods that overwrite a superclass methods.
Now, what changes in a method that uses this annotation to one that doesn't use it? Is this just convention?
Assuming, obviously, that both be methods that overwrite a method from its superclass...
#Override creates a compile-time check that a method is being overridden.
This is very useful to make sure you do not have a silly signature issue when trying to override
It not only makes the compiler check but also documents the
developer's intention.
if you override a method but don't use it anywhere from the type itself, someone coming to the code later may know the purpose. The annotation explains its purpose.
A good IDE will helpfully flag any method that overrides a method without #Override, so the combination of the two will help ensure that you're doing what you're trying to.
it also improves readability

Categories

Resources