aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Is the error due to line *1 or line *2 ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Is the error due to line *1 or line *2 ?" Watch "Is the error due to line *1 or line *2 ?" New topic
Author

Is the error due to line *1 or line *2 ?

Shanel Jacob
Ranch Hand

Joined: Jun 18, 2006
Posts: 112
package father;
public class Father
{
static int familyrank = 1;
}


package son;
*1 - public class Son extends father.Father
{
public static void main (String[] args)
{
*2 -System.out.println("Family Rank = " + familyrank);
}
}
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
1) Use code tags: , put your code between them.
2) If you get a compile error say which message you are getting. You have compiled it haven't you?
3) What does the error tell you? What don't you understand about the error message?


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Nilesh Patel
Ranch Hand

Joined: Feb 02, 2006
Posts: 91
familrrank is not public or protected so you have to give access modifier protected or public for compile and run code..

Bcoz default and private modifier not acess out site of package ..


Nilesh Patel
SCJP 1.5 - 87%
Shanel Jacob
Ranch Hand

Joined: Jun 18, 2006
Posts: 112
So the problem is because familyrank in class Father was set to static instead of protected or public. The Son class can call "familyrank" directly instead of "Father.familyrank" because "familyrank" has been inherited. Thank you for the explanation.

Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
static instead of protected or public


No, static is a totally different qualifer than protected or public. The compiler error is in class Son. The problem indicated by the error message says "familyrank is not public in father.Father. cannot be accessed from outside package". The member familyrank should be protected or public in addition to being static.

If you make familyrank an instance member instead of static you will have another entirely different problem when you make it protected or public. But I will leave it to you to compile and to read the compiler message.
[ July 17, 2006: Message edited by: Barry Gaunt ]
Shanel Jacob
Ranch Hand

Joined: Jun 18, 2006
Posts: 112
Hi Barry, I still don't get it. Use "Father.familyrank" because "familyrank" is static?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
What did the compiler message say when you remove static?
Jeronimo Sanchez
Greenhorn

Joined: Jul 17, 2006
Posts: 23
The problem here is that the static variable "familyrank" is declared with the default access control level (i.e. no access modifier). That means that it's visible only for classes within the same package where it has been declared. And in this case, it's even not inherited.

You can fix the problem by changing the access control level of "familirank" so that in can be inherited by "son", using "protected" or "public".

BR/Jero
[ July 17, 2006: Message edited by: Jeronimo Sanchez ]

Preparing SCJP 1.5
Shanel Jacob
Ranch Hand

Joined: Jun 18, 2006
Posts: 112
No errors when compiling Father.

Error message when compiling son is:
"package father does not exist ... public class Son extends father.Father "

2nd error message is:
"cannot resolve symbol ... System.out.println("Family Rank = " + familyrank);"

So far I understand that the 2nd error message is due to familyrank being declared "static" so it is visible only within the father package. What about the first error message? Can someone explain why "son" cannot see the "father" package?
Jeronimo Sanchez
Greenhorn

Joined: Jul 17, 2006
Posts: 23
Regarding your first error message, I guess you have both classes in the same directory. Try to move son.java to a directory called "son", and father.java to a directory called "father". Then run:



Remember that package names must match the directory structure.
[ July 17, 2006: Message edited by: Jeronimo Sanchez ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
So we have finally got some error messages.

1) the package problem. As Jeronimo wrote, you should be using an appropriate directory structure:



Make sure CLASSPATH is unset and change to the working directory. Compile Father with javac father\Father.java and compile Son with javac son\Son.java

You should now have just the one error involving javarank.

2. The error with javarank is resolved by making javarank in Father.java public or protected. Leave the static in the declaration of javarank.

You can then run the program (from the working directory) with java son.Son


Please confirm that you have done that and got a successful compile and have run the program.
[ July 17, 2006: Message edited by: Barry Gaunt ]
Shanel Jacob
Ranch Hand

Joined: Jun 18, 2006
Posts: 112
Hi Barry,

I added "protected" to "static int familyrank = 1;" in Father. Prior to that, there's only 1 compilation error:

"non-static variable familyrank cannot be referenced from a static context"

I thought the problem was a lack of "protected/public" which prevents the Son class from seeing that Father has this variable called "familyrank"? So what does the compiler mean by saying "from a static context"?

Thank you.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Let's get this straight: do you have


or just

?
Shanel Jacob
Ranch Hand

Joined: Jun 18, 2006
Posts: 112
Hi, I do have



There's no compilation error now (only exception errors).

I was just saying BEFORE I added the "protected", the error message I got was



To which I thought the problem was a lack of "protected/public" initially which prevents the Son class from seeing that Father has this variable called "familyrank"? So what does the compiler mean by saying "from a static context"?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
OK.
compiles, but
does not giving "non-static variable familyrank cannot be referenced from a static context".

This error is in the public static void main method, isn't it? When familyrank is not static it is an instance member, not a class (static) member.
You must have an instance (an object) of Son to be able to access its familyrank member.

So create an instance of Son:
and then access its familyrank:


How does that compile?
[ July 18, 2006: Message edited by: Barry Gaunt ]
Shanel Jacob
Ranch Hand

Joined: Jun 18, 2006
Posts: 112
Hi Barry, it compiles fine.

Since I was able to compile fine with "javac son\Son.java" I did the same thing and get exception error msgs when I try to run "java son\Son" though:



However, if I run it by "java son/Son", then no problem! Am clueless about the difference between forward and back slash I guess
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Well, you should be in your working directory (containing son and father subdirectories) and run with java son.Son (that's a dot, not a slash or backslash).

Shanel Jacob
Ranch Hand

Joined: Jun 18, 2006
Posts: 112
Thanks Barry!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is the error due to line *1 or line *2 ?