Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Package

 
Ron Lam
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can somebody explain why the statement "package packageB;" must be there before SubclassB can compile.

// Filename SuperclassA.java
package packageA;
public class SuperclassA {

public int superclassVarA;

public void superclassMethodA() {
}

}

class SubclassA extends SuperclassA {

void subclassMethodA() {
superclassVarA = 10;
}

}

class AnyClassA {

SuperclassA obj = new SuperclassA();

void anyClassMethodA() {
obj.superclassMethodA();
}

}
=============================================================
// Filename SublcassB.java
// Without this statement, SubclassB.java could not compile
// Error: file does not contain class SuperclassA
package packageB;
import packageA.*;
public class SubclassB extends SuperclassA {

void subclassMethodB() {
superclassMethodA();
}

}

class AnyClassB {

SuperclassA obj = new SuperclassA();

void anyClassMethodB() {
obj.superclassVarA = 20;
}

}
 
Eric Sexton
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where are you putting these files? In other words, what's the folder structure? This may mean that you physically have SubclassB class in the packageB folder. So without putting the right package, a compile error would occur.
[ October 24, 2003: Message edited by: Eric Sexton ]
[ October 24, 2003: Message edited by: Eric Sexton ]
 
Ron Lam
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Eric,
 
Ron Lam
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Eric,
I have put the source files in c:\temp and the generated class files in c:\temp\packageA and c:\temp\packageB respectively.
But, what is the reason for putting SubclassB in a packageB (or it may be other package) for it to be successfully compiled.
Regards.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic