After getting the rest of it all working with samples, I tied to serialize my actual underlying object, and found that it would always kick back an error about not being able to guess about the class I'm trying to serialize. This is highly simplified example of what I'm trying to do, along with annotations that seem to make sense to me for how to do it.
I want to serialize a List<> of primitive (or boxed primitive) objects, in this case one int & one string. My actual class is all primitive (or boxed primitive) types also.
#JSONMapper
public static interface TestMapper extends ObjectMapper<TestElmt>{
TestMapper INSTANCE = new Webworkers_TestMapperImpl();
}
public static class TestElmt {
List<test> inerVar = new ArrayList<>();
public void addElement(test elmt){
inerVar.add(elmt);
}
public List<test> getElements(){
return inerVar;
}
}
#JSONMapper
public static class test{
public static test_MapperImpl MAPPER = new test_MapperImpl();
int x;
String y;
test(int X,String Y){
x = X;
y = Y;
}
}
But the error I get is:
Error:java: error while creating source file
java.lang.IllegalArgumentException: couldn't make a guess for
client.myEnclosingClass.test
The code in the question has two issues that will not allows it to compile :
First the test class should be named as Test - capital T - instead of test - small t -.
Second there should be a no args constructor on class test, otherwise the deserializers wont know how to create a new instance of the class, it will be generated but will have a compilation error in its create method.
if we change the test class like this all should work
#JSONMapper
public static class Test {
public static Test_MapperImpl MAPPER = new Test_MapperImpl();
int x;
String y;
public Test() {
}
Test(int X, String Y){
x = X;
y = Y;
}
}
this is because gwt-jackson-apt makes some assumptions and uses some conventions to generate the underlying serializers/deserializers.
Related
suppose in Eclipse I have three packages with the following classes in each:
Packages: Classes
Head: head.java
Body: arms.java
Legs: feet.java
I want to define class info in brain.java and pass it through methods to the other classes (arms.java and feet.java) and update the contents of info.
class info {
// some vars such as bools,ints,strings
}
For example, have updateArms be a method defined in arms.java. I want to do the following in brain.java:
arms.updateArms( info );
I am having trouble finding how to first define a class that behaves this way, and secondly how to pass it as a parameter to another linked class.
First, you should learn about Java naming convention.
For example, package should be head, and the class should be Head.
Go back to your design: In OOP, we see the program as interactions between object instances.
In your example, it may look like:
class Arm {
void moveUp(SomeInfo info) {
...
}
}
class Brain {
private Arm leftArm;
private Arm rightArm;
void reachForward() {
rightArm.moveUp(...);
}
void connectLeftArm(Arm arm) {
this.leftArm = arm;
}
//....
}
class Body {
Brain brain;
Arm leftArm;
Arm rightArm;
public Body() {
this.brain = new Brain();
this.leftArm = new Arm();
this.rightArm = new Arm();
this.brain.connectLeftArm(this.leftArm);
this.brain.connectRightArm(this.rightArm);
}
}
I wish this demonstrate the difference of way of thinking.
If you start get used to the way OOP see things, then you can take next step in refining your design (e.g. by different design pattern)
You can achieve this using Inheritance.
Inheritance can be defined as the process where one class acquires the properties (methods and fields) of another. With the use of inheritance the information is made manageable in a hierarchical order. The class which inherits the properties of other is known as subclass (derived class, child class) and the class whose properties are inherited is known as superclass (base class, parent class).
Sample Code helps you how to use the methods and properties of other classes.
class Calculation {
int z;
public void addition(int x, int y) {
z = x + y;
System.out.println("The sum of the given numbers:"+z);
}
public void Subtraction(int x, int y) {
z = x - y;
System.out.println("The difference between the given numbers:"+z);
}
}
public class My_Calculation extends Calculation {
public void multiplication(int x, int y) {
z = x * y;
System.out.println("The product of the given numbers:"+z);
}
public static void main(String args[]) {
int a = 20, b = 10;
My_Calculation demo = new My_Calculation();
demo.addition(a, b);
demo.Subtraction(a, b);
demo.multiplication(a, b);
}
}
For example:
In Class One
int killcount = 0;
In Class Two
killcount = 5;
All I want to do I get the variable from one class to another class. How would I do that?
Before trying to work with Bukkit I'd recommend you to get some Java experience first. That's not meant as an insult, but it can get quite confusing if you do it the other way round. Anyways, if you still want to know the answer to your question:
You'll have to create a getter & setter for your "killcount" variable.
class Xyz {
private int killcount;
public void setKillcount(int killcount) {
this.killcount = killcount;
}
public int getKillcount() {
return this.killcount;
}
}
Of course this is a simplified version without checks, but if you want to access the variable from a different class you can create an instance and use the methods to modify it.
public void someMethod() {
Xyz instance = new Xyz();
instance.setKillcount(instance.getKillcount() + 1);
//this would increase the current killcount by one.
}
Keep in mind that you'll have to use the same instance of the class if you want to keep your values, as creating a new one will reset them to default. Therefore, you might want to define it as a private variable too.
Consider the examples
public class Test {
public int x = 0;
}
This variable x can be accessed in another class like
public class Test2 {
public void method() {
int y = new Test().x;
// Test.x (if the variable is declared static)
}
}
Ideally, the instance variables are made private and getter methods are exposed to access them
public class Test {
private int x = "test";
public int getX() {
return x;
}
public void setX(int y) {
x = y;
}
}
Sometimes in a constructor, no statement is given. What does that indicate? For example if i create a class CIRCLE, then inside the class i write CIRCLE() {}, that is nothing is written inside. Can anyone explain it?
If your question is "why would anyone write such a constructor", then the answer is that the no-args default constructor only exists if no other constructor is specified.
Consider the following class.
class Foo {
int x;
}
As written, someone could write the following code to construct Foo.
Foo foo = new Foo();
However, now suppose I added a constructor which takes arguments.
class Foo {
int x;
public Foo(int x) {
this.x = x;
}
}
Now, suddenly, Foo foo = new Foo(); no longer works. To restore it, I must add the empty constructor again.
class Foo {
int x;
public Foo(int x) {
this.x = x;
}
public Foo() { }
}
Now, What if there are no other constructors that take arguments?
In that case, it is generally as the other answers suggest, to restrict access to constructing the class.
In the following definition of Foo, nobody is allowed to construct Foo. Perhaps Foo is meant only as a static class.
class Foo {
int x;
private Foo() { }
}
In the protected case, only subclasses can construct Foo.
class Foo {
int x;
protected Foo() { }
}
If there is no code in the constructor, chances are, it was declared to change the access to the constructor. By default, constructors are public. If you wanted to make it private, protected or package-private, you must explicitly declare it and manually change the modifier.
class Example {
public static void main(String[] args) {
new Demo(); //this is currently allowed
}
}
class Demo {
}
In order to prevent the creation of a Demo object within Example, we could declare Demo's constructor amd make it private:
class Demo {
private Demo() { }
}
Another reason could be that the class has a constructor that requires parameters. If so, you must explicitly declare the no-arg constructor to be able to use it.
If nothing is written, then when a new Object of that type is created, nothing 'extra' is done, whereas if in the constructor has code in, it does something.
For example, the following consructor for a class called 'Bank' assigns the argument 'name' to the field 'bankName', then instantiates a Terminal and 2 bank accounts:
private static final int INITIAL_BALANCE = 200;
public Bank( String name )
{
bankName = name;
atm = new Terminal();
account1 = new BankAccount( INITIAL_BALANCE );
account2 = new BankAccount( INITIAL_BALANCE );
}
It's a default constructor. For instance if you go:
Circle circle = new Circle();
You are then calling the default constructor. When you go ... Circle() that is a call to the default constructor, the one with no parameters.
The point of this is just to 'construct' an object or instantiate a class (instantiate just means create an object which is an instance of the class) with no additional information i.e. parameters.
This would generally be used to initialize fields to their default values, like so:
public Circle() {
this.x = 0;
this.y = 0;
}
public class PolyMorphic {
public static void main(String[] args) {
PolyMorphic.printNumber(new IntNumber(1));
PolyMorphic.printNumber(new DoubleNumber(4.54));
}
public static void printNumber(MyNumber N) {
N.print(N);
System.out.println();
}
public abstract class MyNumber{
abstract void print(MyNumber N);
}
public class IntNumber extends MyNumber{
int x;
IntNumber(){
x = 3;
}
IntNumber(int x){
this.x = x;
}
void print(MyNumber N) {
double temp = (double)x;
System.out.printf("%.2f",temp);
}
}
public class DoubleNumber extends MyNumber{
double x;
DoubleNumber(){
x = 3.23;
}
DoubleNumber(double x){
this.x = x;
}
void print(MyNumber N) {
double temp = x;
System.out.printf("%.2f",temp);
}
}
}
So I am trying to create a method in the PolyMorphic class named printNumber which is polymorphic and can print(to the console) either an intNumber with two decimal places to the right or a DoubleNumber with three decimal places to the right. Such as PolyMorphic.printNumber(new IntNumber(1));
My Problem is this:
On the Lines:
PolyMorphic.printNumber(new IntNUmber(1));
PolyMorphic.printNumber(new DoubleNumber(4.54));
This is the error message:
" No enclosing instance of type PolyMorphic is accessible. Must
qualify the allocation with an enclosing instance of type PolyMorphic
(e.g. x.new A() where x is an instance of PolyMorphic)."
It gives me it for both instances and I am confused to as why It is not working. IF someone could just point me in the right direction I would be really appreciative.
Thank you.
Your inner classes require an instance of your PolymorphicClass because of the way you declared them. However, in your case, you don't need this, so you can mark your inner classes as static:
public static class IntNumber
and
public static class DoubleNumber
This is a Java design feature.
One other solution would be to operate on an instance of PolymorphicClass:
Polymorphic p = new Polymorphic();
p.printNumber(new IntNumber(1));
p.printNumber(new DoubleNumber(4.54));
EDIT:
You also need:
public static abstract class MyNumber
Don't nest your MyNumber class and its daughters inside of Polymorphic.
Nesting classes like that is only appropriate when the nested class (MyNumber, IntNumber, DoubleNumber) is part of the implementation of the enclosing class (Polymorphic). In your case, the only relationship between the two classes is that Polymorphic is calling methods on the Number classes.
By the way, the compiler has already told you one solution to your problem, if you would take the trouble to read and understand what it said. Be grateful, for few compilers are as obliging.
Edit - why is anyone downvoting a reply that is both correct and adds additional information to that provided by other answers?
Does the this prefix for accessing member variables exist in Java?
Here is my HelloWorld code:
public class HelloWorld {
public static int x = 0;
public static void main(String[] args) {
HelloWorld.x = 45;
System.out.println(HelloWorld.x);
}
}
The above code works with/without the class name prefixing the variable x. However, if i try: this.x = 45; or this->x = 45; I receive an error:
non-static variable this cannot be referenced from a static context
I understand member variables can be accessed without the HelloWorld (class name) prefix, like I have done. But, I want to know if the this prefix exists in Java, how do I use it?
EDIT:
Also, could you provide an example where this is appropriate?
duffymo & byte - I greatly appreciate your help. Thanks.
Java has this as a prefix, but it's a reference to the current instance.
Static methods and attributes are associated with a class, not an instance, so you can't use this inside a static method.
public class HelloWorld {
public int x = 0; // note: now it's an instance attribute
public static void main(String[] args) {
HelloWorld hw = new HelloWorld();
System.out.println(hw.x);
}
public int getX() { return this.x; }
public void setX(int x) { this.x = x; }
}
You're attempting to use 'this' to refer to a static, not an instance variable. 'this' is only used to refer to the instance variables of an instantiated object of this class. You cannot use 'this' to refer to static variables on a class.
When you use 'this' you are saying "I want to refer to the variables of this particular instantiation of this class". A static on the other hand is always going to be the same variable for the class, irrespective of instantiation.
In addition the correct syntax for referring to an instance variable is by the dot operator:
this.x = 42; //correct
this->x = 42; //will not compile as its not valid Java
So essentially what you're after is something like the following:
public class Foo {
private int x;
public void setX(int x) {
this.x = x;
}
public int getX() {
return this.x;
}
}
public class HelloWorld {
public static void main(String[] args)
{
Foo foo = new Foo();
foo.setX(45);
System.out.println(foo.getX());
}
}
Remove the static modifier from your variable and then try it with this. Here is the difference:
Static variables exist only once in the whole program. No matter where you are, you could refer to HelloWorld.x and it would always be the same thing. That means, as you've declared it, anyone can modify it too, which may or may not be a good thing.
Member variables (not declared with static) are local to an instance of a class, which means you have to have created an instance with new before you can use it. However, every time you use new to create a new instance of that class, its non-static fields will be different. That is why you have to use this or, if in a different class, a reference to a specific instance in order to access them.
A clarification:
in getter methods it's not necessary to use the keyword this (as it's been shown in other answers). If you're new to Java this will introduce you to variable scopes (local variables vs instance variables). In other words the following works perfectly:
public class Foo {
private int x;
public void setX(int x) {
this.x = x; //Here the keywork this is necessary!
}
public int getX() {
return x; //in this case the 'x' can only be instance variable
}
}
There is also another important use of this for invoking constructors defined in the same class (You might want to check the keyword 'super' as well). Check the following:
public class HelloWorld {
public static void main(String[] args) {
Foo foo1 = new Foo();
Foo foo2 = new Foo(3, 4, 5);
System.out.println("Foo1:\n" + foo1);
System.out.println("Foo2:\n" + foo2);
}
}
class Foo {
private int x, y, z;
public Foo() {
this(-1, -1);
}
public Foo(int x, int y) {
this.x = x;
this.y = y;
}
public Foo(int x, int y, int z) {
this(x, y);
this.z = z;
}
#Override
public String toString() {
return "x= " + x + "\ny= " + y + "\nz= " + z + "\n";
}
}
Enjoy!
You can only use "this" from within an object instance.
"static", by definition, is OUTSIDE of ANY object.
Here's an excellent link in the Java documentation:
http://download.oracle.com/javase/tutorial/java/javaOO/classvars.html
'Hope that helps!
for static variables, use ClassName (like you did). For instance (non-static) variables, use this.variableName
you can use it to access instance variables and methods, you are getting this error because you are using this to access static variables.
and also ... java doesn't have =>, in java you use the .
objectName.variableName = newValue
Use this for getters/setters/constructors.
For example.
class Test {
int x;
int y;
public void Test(int x, int y) {
this.x=x;
this.y=y;
}
public void setX(int x) {
this.x=x;
}
}