This question already has answers here:
What is the point of setters and getters in java? [duplicate]
(14 answers)
Closed 8 years ago.
I'm fairly new to Java -- can someone please give an applicable reasoning for what the Getter and Setter methods's purposes are respectively?
Doesn't it seem redundant to create two different methods in a class to receive data and apply the data? Why aren't they consolidated into one method?
Not at all. A getter can have significantly relaxed implementation to a corresponding setter: the setter may have different access privileges and can prevalidate any of the input data.
This helps achieve much better program stability.
Related
This question already has answers here:
Where can I find the Java JDK source code? [closed]
(11 answers)
Closed 6 years ago.
In java, is there any way to access the methods to classes that already you import? For example, is there a way to view the code for all the methods used for arrays? Such as the constructors, add(), remove(), size()? I have checked oracle, but there is no code, only method names and parameters. I understand how the methods work, but i'd like to see the actual code used.
Search for the JDK source code, depending on the version you want.
This question already has answers here:
What is reflection and why is it useful?
(23 answers)
Closed 9 years ago.
I need explanation for the below questions:
What is reflection in java?
In which situation i need to use reflection?
Real time scenarios and examples for the need of using reflection?
I am very confused in reflection. I had read a lot of documents but I am still confused. Please give me a explanation where I can understand it completely.
what is reflection in java?
Reflection makes it possible to inspect classes, interfaces, fields and methods at runtime, without knowing the names of the classes, methods etc. at compile time. It is also possible to instantiate new objects, invoke methods and get/set field values using reflection.
In which situation i need to use reflection?
mapping objects to tables in a database at runtime.
Real time scenarios and examples for the need of using reflection?
Good Real time example is MyEclipse IDE. if you put your mouse pointer over any class name it reflect the class name and with some information. This is also one type of reflection using reflection api.
Go through this Reflction API link you can get some more info.
This question already has answers here:
Best Practice: Java static non final variables
(7 answers)
Closed 9 years ago.
After a couple of years using Java i've just realized that i don't understand what it the use case for non-final static variables. Can someone give me some hints or any example?
Maybe they are needed to be used in static methods? ... or useful to be shared between all instances?
What concerns me is they can be accessed and modified asynchronously by any subclass, or through any instance.
Thanks.
** note **
Sorry about duplication. I did my search before posting and i didn't find it.
As constants they have no use, which I believe is your main trails of thoughts are concentrated around.
But how do you think a static class is going to perform it's operations if there is any need for class scope variables that need to be shared across the method calls?
Or there are instances where data need to be stored in a static class etc.
There are a lot of use cases if you just stop to think about it.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Get current stack trace in Java
Here is the thing, i want to get a list of all called methods in a java class. So far i've been using eclemma, but that just insn't enough. I also want the order in which the methods have been called.
You can log each method as it is called (with its arguments if you like). You can add a line to each method or use AOP to do this for you.
This question already has answers here:
How to get all enum values in Java?
(8 answers)
Closed 5 years ago.
I'd like to create a JComboBox that handles the selection of any Enum given to it. For that I need a method to retrieve all the available values of the Enum passed to the JComboBox. As I don't know the specific Enum I can't call EnumType.values().
I could think of some complicated solutions where supported Enums would have to implement some interface I define, but I guess I am missing a simpler, more general solution. What is the way I should go?
Class.getEnumConstants()