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.
Related
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 8 months ago.
Improve this question
I have one existing Java 7 method which accepts nothing but return class instance. I want to change it in java 8 using any existing functional interface but don't know what i can use here.
public NotificationPage infraSelection() {
......
return this;
}
Here NotificationPage is class name.
Maybe you can use the Supplier<T> interface?
It declares the method T get().
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 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.
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
I want all my other objects to be able to call one common method. What should my shared method type signature for parameters look like?
public class appSocket{
public appSocket appSocket(TYPE genericInstance){
// Do something with genericInstance here
}
}
In other words, what should "TYPE" look like?
I think you're a bit confused. This just corresponds to the current object. There is no reason to ever say someObject.this.foo(). You'd just say someObject.foo(). You just pass in the object.
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 8 years ago.
Improve this question
This problem is from Question 3.5 in Cracking the Coding Interview 5th Edition. The solutions was like this:
public class MyQueue<T> {
Stack<T> stackNewest, stackOldest;
......
}
So what does two < T > here mean? I searched a lot of pages and didn't get answer. Thanks.
It is the parametrization. It makes use of Java Generics to produce parametrized Classes. In your case, the class MyQueue<T> has parametrization 'T' and inside the class the Stack<T> data structure too has been parametrized with 'T'.
If you are not aware of Generics, I would suggest this article given in the documentation.