Java class attribute name same as classname - java

I have Java class whose classname is same as one of its attribute's name.
Now I get error saying "encountered unrecoverable cycle resolving import" when trying to create object for that class in scala.
public class linkId extends org.apache.avro.specific.SpecificRecordBase implements org.apache.avro.specific.SpecificRecord {
.
.
.
.
.
public linkId(java.lang.String linkId, java.lang.String linkIdScheme) {
this.linkId = linkId;
this.linkIdScheme = linkIdScheme;
}
.
.
.
.
}
[ERROR] [Error]
/Users/dnamburi/.../src/generated/avro/com/rbccm/TradeMessage/linkId.java:15:
encountered unrecoverable cycle resolving import. Note: this is often
due in part to a class depending on a definition nested within its
companion. If applicable, you may wish to try moving some members into
another object.

Related

Error: expected abstract interface package-info {}

We are working on java classes in order to customize it. Before this customization, we just want to check compile/decompile process of existing java jar file (MMC.jar).
We collected all java class file under MMC.jar using jd-gui tool
So when we were compiling the existing MMC.jar (without customization) it is giving attached 6 errors
./com/mmc/model/acknowledgement/package-info.java:7: error: illegal start of
type abstract interface package-info {}
^
./com/mmc/model/acknowledgement/package-info.java:7: error: = expected
abstract interface package-info {}
^
./com/mmc/model/customer/package-info.java:7: error: <identifier> expected
abstract interface package-info {}
^
./com/mmc/model/customer/package-info.java:7: error: illegal start of type
abstract interface package-info {}
^
./com/mmc/model/customer/package-info.java:7: error: = expected
abstract interface package-info {}
^
6 errors
This is the content of package-info java file
package com.mmc.model.customer;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
#XmlSchema(namespace="http://www.iflex.com/mmc/model/customer",
elementFormDefault=XmlNsForm.QUALIFIED)
abstract interface package-info {}
Regards
Ali
The reason you've got compilation error is package-info is not allowed identifier. You cannot name objects like classes, interfaces, variables, etc, with - character. See this answer for more detail Naming rules.
But the problem is that you use package info file in wrong way. The aim of package-info.java file is to add brief description of package in javadoc: what the purpose of this package, responsibilities and contents. You shouldn't declare any classes or interface here. All you need to write in this file is package name in which this file located and package summary javadoc. A #sidgate comment refers to a good answer about package info file responsibility.

Why instance variable with default access modifier can't be found in subclass?

I'm learning about the access modifiers for classes and instance variables. I know that default access modifier can be accessed within the package.
But I can't understand why this code doesn't work:
A.java :
This is the super class file with just one instance variable with default access.
package foo;
public class A {
int a = 10;
}
B.java :
Subclass file within the same package foo, which tries to use instance variable a of superclass class A
package foo;
class B extends A {
public static void main(String[] args) {
B b = new B();
b.test();
}
public void test(){
System.out.println("Variable is : " + a);
}
}
This program is supposed to work, but I got cannot find symbol error.
B.java:2: error: cannot find symbol
class B extends A {
^
symbol: class A
B.java:8: error: cannot find symbol
System.out.println("Variable is : " + a);
^
symbol: variable a
location: class B
2 errors
What is the reason for this error because as per the rule instance variable with default access modifier can be accessed with in a package. Here, the class A is public so it is visible to the class B. Instance variable a of class A has default access, so it can be accessed if the class A is extended by other classes within the same package.
Looks like you've dumped both classes into the current directory (or similar). They will need to be in called foo with correct file name (A.java). And your compiler classpath (or sourcepath) set to the directory containing the foo directory.
A clue is you have two error messages. Usually it's best to sort the first one out first, as subsequent messages may become odd.
Your first problem is that compiler cannot find class A. Once it can do it it can find the member a as well. You problem is probably in the manner you are running java compiler. I believe that you are compiling from command line, aren't you?
In this case you have to be where your source root is. Then run javac foo/B.java. This should work without problems.
While you continue pay attention on command line options -classpath and -sourcepath. Then I'd recommend you to start using IDE.

Java: Import error within single directory?

I'm new to Java.
All of my source files (eg. TreeJPanel.java, Tree.java) are in a single directory called jview, with dependencies between them. When I try to compile with javac jview/TreeJPanel.java I get this:
jview/TreeJPanel.java:39: cannot find symbol
symbol : class Tree
location: class TreeJPanel
protected Tree tree;
^
jview/TreeJPanel.java:41: cannot find symbol
symbol : class Tree
location: class TreeJPanel
public Tree getTree() {
^
jview/TreeJPanel.java:45: cannot find symbol
symbol : class Tree
location: class TreeJPanel
public void setTree(Tree tree) {
There's 15 similar errors. I thought I don't need explicit imports from within the same directory? What am I doing wrong? It is likely my question reveals a lack of conceptual understanding of Java - please feel free to point out. Thanks!
Was Tree.java first compiled into Tree.class? when TreeJPanel.java was trying to compile, it was searching for it.
Try compiling both files together:
javac jview/Tree.java jview/TreeJPanel.java
Cause for this is very simple just u should import the Tree Class into the TreeJpanel Class
Your code should look like this
import jview.Tree;
Class TreeJPanel { ....

cannot find symbol error while compiling a java class containing other class type object

I have a java package which contains two classes. Class A and Class B. I need to create an object of A type in class B. I don't know what is happening. Please someone help me out.
package pack;
class A
class B
I'm using JDK1.5 and tomcat and placed them in java folder in my D drive.
D:\java\jdk1.5
D:\java\tomcat
Right now, my package folder is also in above location
D:\java\pack
Below is how i am compiling my java class files.
Step 1: Compiling A.java
D:\Java\pack>set path=D:\java\jdk1.5\bin (setting up path for jdk1.5 compiler)
D:\Java\pack>javac A.java (Successfuly compiled and formed A.class)
Step 1: Compiling B.java
D:\Java\pack>javac B.java (here, i get an error message )
Below is the ERROR message
Error Message
D:\Java\pack>javac B.java
B.java:9: cannot find symbol
symbol : class A
location: class pack.B
A a = new A(); //creating an object of A type
^
B.java:9: cannot find symbol
symbol : class A
location: class pack.B
A a = new A(); //creating an object of A type
^
2 errors
javac pack\A.java pack\B.java
will do the trick. The compiler has to be able to resolve everything in one invocation. If it's looking for
pack.B
then that corresponds to
pack\B.java
in the directory structure

type definition in a package object "hijacking" the inheritance of a java class in scala code

I have the following situation:
I have a Java class hierarchy like this:
package org.foo.some;
public class Model extends org.foo.some.GenericModel { // ... }
package org.bar;
public class MyModel extends org.foo.some.Model { // ... }
where org.foo.some.Model and org.foo.some.GenericModel are out of my reach (not my code). In Scala, also out of my reach, there is:
package org {
package foo {
package object some {
type Model = org.foo.some.ScalaModel
}
}
}
This leads to a funny behavior in Scala code, e.g.
val javaModel:MyModel = new org.bar.MyModel()
trait FooTrait[T <: org.foo.some.GenericModel] { // ... }
class FooClass extends FooTrait[MyModel] { //... }
does not compile and raises the following error:
type arguments [org.bar.MyModel] do not conform to trait FooTrait's type
parameter bounds [T <: org.foo.some.GenericModel]
Further, I can't invoke any method of org.foo.some.Model nor of org.foo.some.GenericModel on javaModel:
javaModel.doSomething()
raises
value create is not a member of org.bar.MyModel
I am under the impression that the package object is "hijacking" the visibility of the Java class hierarchy in Scala code. Indeed, ScalaModel does not extend org.foo.some.GenericModel.
Is there maybe a way to still access the hierarchy from within Scala code?
Edit: when re-compiling the code out of my reach and removing the type re-definition, everything works. So I think what I'm looking at is a way to "disable" an package-level type definition for a specific class.
Are you using a GUI (in particular Eclipse) to build your project?
This seems related to Scala trouble accessing Java methods (that has no answer but where the general consensus is that the problem is not with scala but with Eclipse).

Categories