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 5 years ago.
Improve this question
I was looking through the documentation for primitive wrappers in Java.
While I understand the utility of all the extra functionality provided by the methods, I don't seem to understand how the object stores the primitive in the first place. There doesn't seem to be any primitive final int.
EDIT: I learnt that Java documentation only shows public fields and methods, and after going through source code see a private int field.
Just to confirm, it's then as simple as the compiler doing autoboxing/autounboxing through the public constructor to set the value ?
If you go to the source code of java.lang.Integer you will find
private final int value;
The reason that you don't see it in the API documentation, is that private attributes and methods are not included in the documentation.
Related
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 12 months ago.
Improve this question
When programming in OO languages like C# or Java, is there a good situation where declaring a public field inside a class is actually valid (I myself always use a property for not making the user of the class depend on the data and to support data protection)?
Otherwise, it feels weird that C# for example allows you to do so.
According to the C# coding conventions public field should be used sparingly:
// A public field, these should be used sparingly
public bool IsValid;
Why? I think because of:
can be edited by any other user of class
if you want to add some logic to field, then you need to create property instead of field. By doing this, you will break a contract of class
it is not possible to override variable
However, there is a case when you need to have field as #VGR said:
public const string foo = "";
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 2 years ago.
Improve this question
I have 2 custom classes A and B.
Now I have an object of A let say 'a' and have to convert it to B class.
Now I have 2 approach
First is I write a transform Util which has a static method for conversion.
The second approach is to write that logic in class A with a method convertToB()
Which one is more accurate. Please suggest.
In short, it depends on the relationship between A and B.
If B extends A the conversion is done within the language when assigning an A object to a B object.
Otherwise, if there is no inheritance at play, it would be advised to either create a util class to do the conversion for the case of future changes, or have a constructor of B that has an A object as a parameter.
This is a matter of preference and a question perhaps better suited for the software engineering/design counterpart to Stack Overflow. That said, I would use the first option. If you ever add more classes and need to convert between them, you will bloat your classes with all the conversion methods. If you have one or more classes with the conversion methods, the code is cleaner.
Separation of Concerns - util class is the best approach
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
I've got an object that is interface, let's call it MyInterface something.
My interface is empty and is being implemented by two classes.
First on has one variable int x, and the second one two: int x, int y.
Variables are private but they've got "get" methods and i know them.
Is it possible to get to the variable of a class just by using "something"?
No, it's not possible (excluding reflection magic), unless you add a getX() method to MyInterface. Then, it's easy and it has the nice side-benefit of being a correct design.
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 7 years ago.
Improve this question
As I know Apache Spark is written in Scala. But its functionality is also exposed as a Java API[1], which in turn can be used in Java programs.
How is this done? Can someone explain me using an example.
In other words if I write a Scala program, and I want to expose it as a java API, what steps should be taken?
[1]http://spark.apache.org/docs/latest/api/java/
It will surely depend on the type of api you are exposing, but in general
Simple methods can be called as is: class A { def doSomething(s: String) } in class A can be called just like a regular java method new A().doSomehing("hello");
Default parameters in methods will not work. You will always have to call the method with the whole parameter list.
traits with behaviour can't be implemented, but if you have common combinations you could create an abstract class and then you can extend that in java. Not sure if this will be solved with default methods for java8 in scala2.12
For object functions you need to call using the escaped path. e.g. object Container { val answer = 42 } you get the constant value from java like int answer = Container$.MODULE$.answer;.
As I said first, it mostly depends on the api you want to expose from scala to java. If you have more specific cases I can edit them in this answer.
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
In this book that I'm using to brush up, there's a relationship between generic methods and the static keyword. It appears that generic methods require that keyword, but I don't see why it's required?
Here's what the book did:
static <T> void myFunction(T [] myArray) {
//......
}
There is no requirement that generic methods be static. See Section 8.4.4 of the Java Language Specification for details (including links to other relevant parts of the spec). At the same time, there's nothing wrong with a generic method being static (or vice versa), either. It all depends on the design requirements.