Java Interfaces , Declaring an Interface [closed] - java

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.

Related

Why we cannot add default for method in class [closed]

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.

Trying to make an ArrayList; compiler cannot resolve "add" [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
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.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I'm supposed to make an Array of objects that will be created on a loop, but for some reason, I can't add things to the array.
I have this
public class _10Weather {
List<Weather> daysoff = new ArrayList<Weather>();
}
When I do
daysoff.add
it says that it can't resolve add. I've been reading a lot about arraylists and questions here on how to add, but it seems that it should work just like that.
Edit: Those are the imports, I've been trying a ton of things.
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
When you write
daysoff.add
then you're asking the compiler to look for a field named add; a data member. The java.util.List class does not have a field named add, so it reports that it cannot resolve this.
You should call
daysoff.add(someInstance);
where someInstance is an instance of Weather.
Then, the compiler will recognize that you are calling the method named add.
By the time you do this, you already have defined someInstance. Adding the instance is separate from creating it.
Let's say that the constructor of Weather takes a string parameter. You'd get something like this:
Weather someInstance = new Weather("sunny");
daysoff.add(someInstance);
In the first line, you've created the instance. In the second one, you're using it.
In this case, it's also important to make sure you have the right type of List; Java AWT also has a class named List, wich is different from java.util.List. The reason people asked you to tell us your imports, is so that they could see if you weren't accidentally using java.awt.List instead of java.util.List.

Workaround for stupid class names [closed]

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
In our company, we have a rule which says that all files must have a 8 letters prefix in their names.
For instance, Blarghhh_MyFile.java.
Because of Java's limitation that a public class must have the same name of its file, our classes have stupid names. For instance:
Blarghhh_MyClass myObject = new Blarghhh_MyClass();
Instead
MyClass myObject = new MyClass();
Does someone know a workaround to solve that?
You cannot have an alias for classes in Java. But you could simple create a new class and let it extend your old. Something like:
MyLongNameClass.java
public class MyLongNameClass{
//stuff
}
WorkingClass.java
public class WorkingClass {
private void coolFunc(){
MyClass mc = new MyClass();
}
private class MyClass extends MyLongNameClass{}
}
But i would rather type the whole name then do it that way.

How do I cast Stream.empty() in Java 8? [closed]

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
All the examples I've found use other type information to infer the type of Stream.empty(). It seems like there should be syntax to allow me to cast it directly. This example works:
import java.util.*;
import java.util.stream.*;
class OptionalBasics {
static void test(Optional<String> optString) {
if(optString.isPresent())
System.out.println(optString.get());
else
System.out.println("Nothing inside!");
}
public static void main(String[] args) {
Stream<String> s = Stream.empty();
test(s.findFirst());
test(Stream.of("Epithets").findFirst());
}
}
/* Output:
Nothing inside!
Epithets
*/
But notice that I have to create s separately in order to provide type information for Stream.empty(). I'd like to just create it in the call to
test(), something like this (which doesn't work):
test(Stream<String>.empty().findFirst());
Is there a syntax for setting the type on the call to Stream.empty()?
You just have the brackets in the wrong spot. Try Stream.<String> empty().findFirst())

NoSuchMethodException in Intellij in basic project [closed]

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
I just started to learn Java and I want to use Intellij however I'm not able to run any project(even a simple Hello World). I always get this Exception.
Please follow proper syntax for the main method . .
public static void main(String[] args) {
//Your code
}
You have used method name as Main change it to main it will work.
You have a typo in the method name. It must be 'main' always because Java is case sensitive and JVM always looks for a 'main' method to run.
Try this:
public static void main(String[] args) {...}

Categories

Resources