Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
If we are not adding any access specifier to method by default it is of default type.
but we are adding default keyword then it is giving me error, like we can have default method in interface only. I am aware about the default method in functional interface but strange why it is showing error in eclipse.
public class Test
{
default void test() { //Default methods are allowed only in interfaces.
}
}
this one is working fine
public class Test
{
void test()
{
}
}
any reason for it?
The default keyword has nothing to do with default 'package private' access that derives from not specifying the access. The default keyword only applies to interfaces (and not just to functional interfaces), to supply a default implementation in the interface. This is a feature introduced in Java 8 to allow for easier interface evolution.
Overloading the default keyword to mean 'package private' access when used in classes would only be confusing and serve no real value, as the same is achieved by not specifying access.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
Can java interface methods can only return primitive types (int,string,etc?)
I want to have a method return a type "BitMap". But my IDE is complaining with Cannot resolve symbol "BitMap":
public interface MyContract {
public BitMap getBitMap();
....
I suspect I'm misunderstanding something inherent to interfaces that is preventing me from doing this?
Can java interface methods can only return primitive types (int,string,etc?)
No, they can return any valid data type.
I want to have a method return a type "BitMap". But my IDE is complaining with Cannot resolve symbol "BitMap":
There is no type named BitMap visible to this Java source file. You are missing an import statement. And, perhaps, there is a typo in the type name. For example, on Android, android.graphics.Bitmap does not have a capital M in its name.
Interfaces can return any object type.
Try Bitmap instead
There is an error and a suggestion right here assuming you meant the standard Bitmap class and not a custom one:
BitMap is written Bitmap
Interfaces have no privacy keyword so you can remove public from your method since it's redundant.
This should be the result
public interface IMyContract {
Bitmap getBitMap();
....
}
Anyway you can return any type you want from an interface, there are no limitation of this kind
You can return a reference type (for example, try with Object).
Check for import the package in which the class is defined.
Check whether the class has the appropriate access modifier after you have inserted the package.
Can java interface methods can only return primitive types (int,string,etc?)
It doesn't matter whether you are returning an Object or a primitive for an abstract method in Java.
Make sure you are making the necessary imports or typing the right class name, it seems that your IDE problem is somewhere else.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Why eclipse throws an error message while giving public modifier to interface
publid interface one // throws an error
{
}
Public type one must be defined in its own file error i am getting
please give your clarifications regarding this
Create a new file called 'one.java'. Place the declaration of your interface in there.
Every public class, interface etc. needs to be in its own file.
Because your Java file containing one most probably is not named "one.java".
Any public class or interface must be declared in a separate file, having the name of that interface or class.
If you have multiple top level classes/interfaces in the same file, only one of them can be public.
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 years ago.
Improve this question
As we know that in java 1.8 static methods are allowed in interfaces , I have seen some answers like static methods defined in interface from jdk 1 8 why did they need to do so
but I am not satisfied. Furthermore I think it may cause problems like :
public interface MyInterface{
public static void myMethod();
}
class MyClass{
MyInterface.myMethod(); // since myMethod is static but a huge error is waiting for us here ?
}
But I still think there is a way out of this since this is added by professionals , so can anyone please explain how oracle solves this issue and what is the need to add this ?
Thank you in adavance.
I have not used java 1.8 so I never knew that static methods in java needs to be defined not just declared , I always thought of the Interfaces as a Pure Abstract Class I think that's why the idea of defining a method seemed strange to me . Thank you for your help ! .
Talking about "what is the need to add" static methods:
Quoting from http://www.informit.com/articles/article.aspx?p=2191423
Before Java 8 made it possible to declare static methods in
interfaces, it was common practice to place these methods in companion
utility classes. For example, the java.util.Collections class is a
companion to the java.util.Collection interface, and declares static
methods that would be more appropriate in the relevant Java
Collections Framework interfaces. You no longer need to provide your
own companion utility classes. Instead, you can place static methods
in the appropriate interfaces, which is a good habit to cultivate.
Also static methods in interfaces are good for providing utility methods like null check, collection sorting etc. And importantly it provides security by denying implementation classes from overriding it.
There's no problem here, the static method is owned by class, not it's members, so the only error here is the fact you didn't defined the method itselt (just declared it, which is not allowed with static methods).
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I see enum usage like below.
public enum MyEnum {
ENUM1(1),
ENUM2(2),
ENUM3(3);
private int enumValue;
MyEnum(int i) { this.enumValue = i; }
public int getEnum() { return this.enumValue; }
}
// this is the call
MyEnum.ENUM1.getEnum()
When above call is made, constructor of the MyEnum is called three times. All I want to take is a value but three instances are created! Isn't this a bad thing or am I doing something wrong?
EDIT: What I understand is that when the second call is made MyEnum.ENUM3.getEnum(), no more new instance is created.
You have three enum constants
ENUM1(1),
ENUM2(2),
ENUM3(3);
For each constant, the constructor needs to be invoked. This is normal behavior.
The constants are all initialized when the enum class is loaded and initialized.
The three instancee ENUM1, ENUM2 and ENUM3 are created - due to your declaraton of MyEnum when the class is loaded. Your call simply retrieves the int associated with ENUM1.
This is the way Java's enum is defined. You can't change it.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I know that in JavaScript, you don't need the public keyword in the following code :
class myClass
{
public int myVariable;
// it is the same as :
int myVariable
}
do you need it in Java ? What is its purpose ?
Yes if you want something to be accessible everywhere.
Otherwise it is package-visibility, meaning only stuff in the same package (at some level) can access it.
You don't 'need' the 'public' keyword - if you don't specify the access level of a Class variable it will be set to package-private.
More details are here -
http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
public fields are a bad idea, however (A VERY bad idea in a multi-threaded application). It allows other classes to change the state of your class without any control, and could break invariants. The proper way is to control state-changing through public setter methods.