Could not find any documentation that explains why class variables are not accessible from within enums. Consider this example:
package groovy;
public class Universe {
static String test = "test";
enum Planet {
EARTH {
#Override
void doSomething(){
System.out.print(test);
}
};
abstract void doSomething();
}
public static void main(String[] args) {
Universe.Planet.EARTH.doSomething(); // No such property: test for class: groovy.Universe$Planet$1
}
}
I am aware that this code is also perfect Java code. Though it works as expected using JRE but not with Groovy's runtime environment which makes me even more curious. Is there any proof for the difference? Thanks
Groovy needs some help to find Universe's variable. Here's a link to some official documentation if you want to read up on a few differences between Java and Groovy. Here's an email discussion that touches on this some. For anyone unfamiliar with enums, they're static objects, so no accessing instance objects or variables. Which leads us to our answer, static variable needs to be referenced in a static way (via class, not an instance).
Also, if you try to use inner classes before Groovy 1.7 you're going to have a bad time (you can't).
public class Universe {
static String test = "testing";
enum Planet {
EARTH {
#Override
void doSomething(){
System.out.print(Universe.test);
}
};
abstract void doSomething();
}
public static void main(String[] args) {
Universe.Planet.EARTH.doSomething(); // No such property: test for class: groovy.Universe$Planet$1
}
}
Related
I have a class -->
public class Machine
There I have declared a static inner class -->
public static class Parts
Inside static inner class I have declared two static methods -->
public static void engine()
public static void battery()
Now I want to access the methods from my main class App. I am using Eclipse IDE. I did -
Machine.Parts machine = new Machine.Parts();
machine.engine();
machine.battery();
Eclipse is letting me to do it. But it is giving me warning -
The static method engine from the type Machine.Parts should be accessed in a static way
The static method engine from the type Machine.Parts should be accessed in a static way
How to resolve this problem?
I have tried google search and stack overflow previous questions. But nowhere I could find the solution.
My code -
Machine.java -->
package demo28;
public class Machine {
public static class Parts {
public static void engine() {
System.out.println("Machine engine is running");
}
public static void battery() {
System.out.println("Machine battery is charging");
}
}
}
App.java -->
package demo28;
public class App {
public static void main(String[] args) {
run(new Machine.Parts());
}
public static void run(Machine.Parts machine) {
machine.engine();
machine.battery();
System.out.println();
}
}
Output -->
Machine engine is running
Machine battery is charging
Expected --> No warning
Actual --> Getting warning
Here:
Machine.Parts machine = new Machine.Parts();
You are creating an instance of that inner class. Then you go:
machine.engine();
... invoking a static method, as if it were a non-static method of that class.
Yes, the above code works, but it is bad practice. If you meant to have "real" non-static methods, simply drop that keyword from the method signatures. Otherwise, change your code to:
Machine.Parts.engine();
Because that is what really happens in your code example.
I am developing a plugin for an RCP application.
Within the plugin.xml, I need to register certain classes at a given extension point.
One of these classes is an anonymous (?) class defined like this:
package de.me.mypackage;
import org.something.AnotherClass;
public class ClassOne {
...
public static AnotherClass<ClassOne> getThat() {
return new AnotherClass<ClassOne>() {
...
};
}
}
Is there any way to reference AnotherClass<ClassOne> within the plugin.xml?
I already tried something like de.me.mypackage.ClassOne$AnotherClass but that does not work. Do I have to declare that class within its own file to be able to reference it?
As far as I know, it would have a numeric index:
class Bla {
public static void main(String[] args) {
(new Runnable() {
public void run() {
System.out.println(getClass().getName()); // prints Bla$1
}
}).run();
}
}
After compiling, you get:
$ ls *.class
Bla$1.class Bla.class
That said, you can't rely on the numbering in case the source file is modified.
Can you instead define a static inner class, like:
public class ClassOne {
public static class MyClass extends AnotherClass<ClassOne> {
public MyClass(/* arguments you would pass in getThat()? */) {
...
}
...
}
public static AnotherClass<ClassOne> getThat() {
return new MyClass(...);
}
}
I need to say the obvious here - you should make it a named class if you want to refer to it. Whether you can access it otherwise is a technical curiosity (that I don't happen to know the answer to), not something you should actually do in production.
The dollar sign only comes into play in the class's binary name; in Java source, just use de.me.mypackage.ClassOne.AnotherClass.class.
I'm still a bit knew to Java. Only done PHP and a bit of javascript most my life. I had a question concerning the order you have your classes/methods encapsulated inside each other. Does it matter? Is there a better professional way with doing this? Sorry for the newbie question.
Method 1 (class after main())
public class myClass {
public void main(String[] args) {
//Body Code Here
}
}
class myClass {
//Body Code Here
}
Method 2 (class inside main())
class mainClass {
public void main(String[] args) {
//Body Code Here
class myClass {
//Body Code Here
}
}
}
Method 3 ( class before main())
class myClass {
//Body Code Here
}
class mainClass {
public static main(String[] args) {
//Body Code Here
}
}
You need to have all your methods inside the body of your class. Outside it, you can only import some libraries for example java.util.Scanner(for reading from the console).
And you can insert methods before or after the Main method, both ways when you run the program they will be read.
Your main method, as any other method, must be within a class. Therefore only the first method would work.
Your first method will work.
as the main method should be in the class then only jvm will compile it.
I understand how to use and import outside packages, but I've never packaged my own classes before. I read the Oracle Tutorial on Creating a Package, and looked at In Java, what's the difference between public, default, protected and private in addition to several sites/SO threads on packages. For the life of me, I can't figure out why this extraordinary simple example doesn't work:
package PTest;
public class A
{
protected final int SIZE = 10;
public void printSize()
{
System.out.println(SIZE);
}
}
package PTest;
public class B
{
public static void main(String[] args)
{
System.out.println(SIZE);
hello();
}
}
I used eclipse's autopackaging feature, so I assume that the actual packing is correct. Here's an image to show that they are indeed packaged correctly:
As you can see, neither the protected SIZE or the public hello() are recognized. I've tried this outside of eclipse, also to no avail. Any help would be greatly appreciated.
SIZE is an instance field of A objects. You need to make it a static field. Even then, it'll be a member of the A class, so you have to specify A.SIZE to use it in B.
Class methods cannot access instance variables or instance methods directly—they must use an object reference.
Errors you getting are fixed here
package PTest;
public class B
{
public static void main(String[] args)
{
A MyClassA = new A();
System.out.println(MyClassA.SIZE);
MyClassA.printSize();
}
}
You can not directly access methods or fields which are not static (instance members) being in a static scope(main) other than using an object and then access or making those instance members as staic.(class members)
I just found a Java example that uses variables typed as the current class itself. I can't understand why and when to use something like this! It's not explained by the author of the book because it's just a part of code of an example about other stuff! Could anyone help me to understand the utility of this approach? Is it related to something like "Singleton design pattern"? Furthermore I also tried to instantiate test1 and test2 but I got an error!
public class Test {
public Test() {
Test test1;
Test test2;
}
}
The original snippet is about nested classes:
public class Tree {
ExampleNode master;
public Tree() {
}
//...
class ExampleNode {
ExampleNode rightNode;
ExampleNode leftNode;
//...
void printMaster() {
System.out.println( master );
}
}
}
A simple example of where this would be useful is in a linked list, where each node needs a reference to its neighbour(s).
Use can create object of a class inside that class to call a class method. Consider the following example:
public class SomeClass {
public void callMethod() {
}
public static void main(String... args) {
SomeClass sc = new SomeClass();
sc.callMethod();
}
}
We cannot call a non static or instance method from a static method without using the instance of the class where the method belongs to. Right?
There is no relation with Singleton Pattern.
The one you need to instatiate is Test. As i see there's no relation with Singleton pattern, don't you miss any code?