File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confusion Question

 
Venkat01
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
given these two source files
F1.java
Package forms;
Public class F1{ int type;}
F2.java
Package forms;
Class F2{
Int gettype(F1 ques)
{return ques.type;}
}
1. both compile successfully
2. Neither compile successfully
3. Only F1 compiles
4. Only F2 compiles
Totally confused with the question.
 
Tony Alicea
Desperado
Sheriff
Posts: 3226
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't mean to be picky, but nothing will compile. Java is case sensitive. Who knows if, in the exam, you may get a question that depends on you knowing just that?
Can you repost with the case corrected?
 
paul wheaton
Trailboss
Pie
Posts: 21187
Firefox Browser IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where did the question come from?
Tony's right. If that's the question verbatim, then neither will compile cuz there is no keyword "Package" although there is a keyword "package".
If we ignored case, I would say the answer is 3 because of the statement "return ques.type;"
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ignoring case (which applies to "Class", "Public" and "Int" as well as "Package"), then the correct answer is 1 - both compile successfully. (Yes I did it myself to be sure.) There's nothing wrong with the line "return ques.type;" - ques is declared as an instance of F1, and type is a member variable of class F1 which has package access. Since both classes are in the same package, this is fine. It's true that we often see only private variables (or public static final), but that isn't always the case.
 
paul wheaton
Trailboss
Pie
Posts: 21187
Firefox Browser IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doh!
Yer right. I just didn't see it.
Once again the compiler shows the proof.
 
Siddhu
Ranch Hand
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Able to compile F1.java
Unable to compile F2.java
Got the compilation error as:
F2.java:3: Class forms.F1 not found.
int gettype(F1 ques) {
F2.java:4: Class forms.F1 not found in int gettype(forms.F1).
return ques.type;
2 errors.
Any help?
F1.java
package forms;
class F1{
int type;
}
<BOLD>F2.java<BOLD>
package forms;
class F2 {
int gettype(F1 ques) {
return ques.type;
}
}
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Siddhu- the problem is probably the form of the javac command you use. There are several ways to set it up, but try this:
<code>javac -d . F1.java F2.java</code>
The "-d ." tells the compiler to put the class files in appropriate subdirectories for the package they're in, which in turn allows the compiler to more easily locate the classes it needs later when a different file tries to access them. You need to compile F1 before (or simultaneously with) F2, or you need to have the .java files in package-specific subdirectories as well (again, so the compiler can find them).
 
Venkat01
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks every body for the reply and sorry for the typo. I promise to be more careful in future. Gentlemen, please understand that this is not deliberate.
Again thanks JIM, you have been wonderful. One small point, have you stopped answering posts in javacert.com. if so then there is no need to go there.
Byeeeee
 
Siddhu
Ranch Hand
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got a compilation error as when it tried to compile the below code.
Class1.java:2: Superclass Class1.Base of class Class1.Class1 not found.
public class Class1 extends Base {
I tried to compile using javac -d . Base.java Class1.java
<BOLD>
Base.java
package Base;
class Base {
protected void amethod() {
System.out.println("amethod");
}
}
Class1.java
package Class1;
public class Class1 extends Base {
public static void main(String[] args) {
Base b = new Base();
b.amethod();
}
}
 
Tony Alicea
Desperado
Sheriff
Posts: 3226
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to specify the package that the Base class belongs to:
<PRE>public class Class1 extends Base.Base</PRE>
The classes are not in the same package.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...or you need to add the line
import Base.*; // (or Base.Base)
to the file Class1.java
Venkat01 - Thanks. I'm not yet planning to stop posting in javacert entirely, but I am planning to shift most of my posting here. Someone's got to look after Tony after all.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic