Workaround for stupid class names [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
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.

Related

in Java I get an error, that says close parenthesis is expected [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
my Main class look like the following:
public class Main {
public static void main(String[] args)
{
Account account=new Account();
account.deposit(amount:10);
System.out.println(account.getBalance());
}
}
and Account class as below:
public class Account {
private float balance;
public void deposit(float amount){
if(amount>0)
balance+=amount;
}
}
I get an java ) expected error, which refers to the line "account.deposit(amount:10);" in Main class and hints, that amounts can not be resolved, but i dont understand why, could you give me some hint.
Change
account.deposit(amount:10);
to
account.deposit(10);
or
account.deposit(10.0f);
You can't label parameters that way at the caller's site. Also, don't forget to implement getBalance(). And use brackets ({}) even when they're optional.

Minecraft Java Minecraft.getMinecraft cannot be resolved to a type [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 4 years ago.
Improve this question
I recently started coding a mod for Minecraft and I can't get past this one stupid error. "Minecraft.getMinecraft cannot be resolved to a type". I don't know how to fix this, I was watching a tutorial and I had the same version and everything.
I checked all the code, and everything is right. If someone could help that would be much appreciated since I really don't want to give up and I really want to finish this mod. Thanks!
The module code where I get the error:
package module;
import net.minecraft.client.Minecraft;
public class Module {
public Minecraft mc = new Minecraft.getMinecraft();
private String name, displayName;
private int key;
private Category category;
private boolean toggled;
}
If you want to call the constructor of Minecraft it should look like:
public Minecraft mc = new Minecraft(...);
If you don't want to call constructor but use some static method of the class Minecraft called getMinecraft() you should remove the "new" keyword like Asier Aranbarri said like this:
public Minecraft mc = Minecraft.getMinecraft();
The following code should work:
public Minecraft mc = Minecraft.getMinecraft();
getMinecraft() is a static method of the Minecraft class, so it is called using Minecraft.getMinecraft(). The new keyword is only needed when instantiating a new Object, which is not the case here.
The code examples on Java Code Examples for net.minecraft.client.Minecraft agree with the comment by Asier Arranbari: It should be
public Minecraft mc = Minecraft.getMinecraft();
That is, without new.
What happened?
When you did new Minecraft.getMinecraft(), the compiler assumed that Minecraft.getMinecraft was a class that you tried to instantiate by invoking a no-arg constructor. That is, a class named getMinecraft inside the Minecraft class. However, getMinecraft is a (static) method, not a class. Therefore you got the message “Minecraft.getMinecraft cannot be resolved to a type”.

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())

Java Interfaces , Declaring an Interface [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
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.

Java enum constructor is called multiple times [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 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.

Categories

Resources