• Post Reply Bookmark Topic Watch Topic
  • New Topic

Basic Java extend class question  RSS feed

 
Richard Pan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
// single file below, named A.java keeps generating compilation errors, WHY?

//modified from D. Flanagan, Java in a Nutshell, 5th ed, pg 121
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

What compilation errors? Don't make us guess.

Also, please be sure to use code tags when posting code to the forums. Unformatted or unindented code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please click this link ⇒ UseCodeTags ⇐ for more information. Properly indented and formatted code greatly increases the probability that your question will get quicker, better answers.

I've gone ahead and added the code tags for you. See how much easier the code is to read?
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

You have statements that are not in a method. Put your System.out.println in a main() method.
 
Richard Pan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute, I followed your suggestion that ACTIONS ON OBJECTS are put in methods and when done so properly, my program now compiles and executes without error.
class T { int x =1, y=2;
public void prt() {
System.out.println("T class ending: x= " +x+ ", y=" +y); }
}
class S extends T {
int m = 10;
int n = m + x +y;
public void prs() {
System.out.println("S subclass ending: m = " + m + ", n=" + n); }
}
public class A{
public static void main(String[] arg) {
System.out.println("****Program start");
T t1 = new T();
t1.prt();
S s1 = new S();
System.out.println(s1.m);
System.out.println(s1.n);
s1.prs();
System.out.println("****Program ending");
} }

However, is it true my prt() method MUST be called from the instantiating method, main() here?
Can my prt() method be called FROM WITHIN CLASS T ITSELF, using "this" or other constructs? ie can a class invoke its own method?

Thank you for giving a thorough answer!!
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard,

Be sure to UseCodeTags (← that's a link) when posting your programs. You'll get better responses.

Can my prt() method be called FROM WITHIN CLASS T ITSELF


I'm not sure what you mean. A method can call another method in the same case. And you need a main() method to start processing.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think the UseCodeTags link worked above... You can find the actual page here
 
Richard Pan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please allow me to try another time:


above code appears in one single file named A.java.

Thanking you in advance! Richard C Pan
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

You have already been told twice that statements must be inside methods (or constructors or initialiser but you probably haven't seen initialisers yet). You need to move that line before the preceding }
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!