The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Re: Assigning a Value to a Class extending a Superclass

cs13,
Thank you so much. I tried that and it worked so that you so much.

KevinWorkman,
Your method is also working. I just replaced "public" with "static" and my original code is working again.
I just want to say that cs13 helping is not a spoon-feeding way. Yes, he gave me the code but as a java programming self-studying person, I will surely go dig for the reason for this kind of syntax including "static".

I got The Oracle Java online tutorial as a .chm book and I haven't reached "Nested Classes" yet, as you see:Course.chm.jpg

Re: Assigning a Value to a Class extending a Superclass

I just want to point out that it was copeg's suggestion, not mine (I don't disagree with him, I just don't want to take the credit).

And it's good that you can learn from code like that, but you're in the minority. There is a non-code, non-teachable skill of problem solving that is only perfected through practice, and full-code solutions like cs13's rob people of that practice.

Also, my real problem with cs13's approach is that it's not exactly the correct answer, and he continues to post slightly-off solutions. In his example, the inner class does not rely on anything in the outer class, but by making it non-static, he's adding the overhead required to keep around the unused references to the outer class. Making the inner class static is the way to go in that case.

Re: Assigning a Value to a Class extending a Superclass

Well Kevin, I don't think I am that skilled to judge that but it seemed to me as a new approach of accessing nested classes. The truth is that I didn't consider the AndroidCon as a nested class but a subclass inheriting from the superclass. The issue with the Oracle online tutorial and thus the book version is not telling about the structure. Yes, they tell you how to let a subclass inherit the superclass but they don't explain the structure.

I've been googling a lot on classes structure but I have nothing. I mean questions like:
- Can I write a class outside the superclass which its end "}" comes in the end of the public static void main..?
- Can I write a class in a new .java file and make it inherit objects from the superclass which is written in another .java file within the same project?

Actually a lot of questions all about the structure. I understand the syntax but the structure is missing from Oracle online tutorial.

However, thank you so much again for the help and I am really glade to find this great forum. I just hope to be a more active and useful to the other members as I am learning.

Re: Assigning a Value to a Class extending a Superclass

Originally Posted by ubermoe

Well Kevin, I don't think I am that skilled to judge that but it seemed to me as a new approach of accessing nested classes. The truth is that I didn't consider the AndroidCon as a nested class but a subclass inheriting from the superclass. The issue with the Oracle online tutorial and thus the book version is not telling about the structure. Yes, they tell you how to let a subclass inherit the superclass but they don't explain the structure.

I see. In your first example, you had your subclass inside your superclass, which makes your subclass also an inner class, which slightly complicates things. If your question has nothing to do with nested classes, I would take out the nesting.

Originally Posted by ubermoe

I've been googling a lot on classes structure but I have nothing. I mean questions like:
- Can I write a class outside the superclass which its end "}" comes in the end of the public static void main..?

I'm not sure I understand the question. What happened when you tried? Are you perhaps talking about anonymous inner classes?

Originally Posted by ubermoe

- Can I write a class in a new .java file and make it inherit objects from the superclass which is written in another .java file within the same project?

Sure! But again, what happened when you tried?

Originally Posted by ubermoe

Actually a lot of questions all about the structure. I understand the syntax but the structure is missing from Oracle online tutorial.

Many of your questions are going to be answered simply by writing a small program (an SSCCE) and trying it out.

Originally Posted by ubermoe

However, thank you so much again for the help and I am really glade to find this great forum. I just hope to be a more active and useful to the other members as I am learning.

I follow the sections provided but to take an example and let it be my code from the first post. I have a superclass called
ObjectsCon and a subclass (practically nested class as you mentioned) called Android. The final code is:

As you can see. the close of the superclass is not after the last statement of it, but it's the last "}" in the code. The bold ones.
Lets try to do a re-arrange of the code and move the subclass a little bit out side the superclass as the following:

What I have done is simply, taking my subclass "Android" to outside the superclass and changed it from "static" to "public" so it can be accessed globally. but the error is now about:

public class Android extends Objects{

and it says: class Android is public, should be declared in a file named Android.java
that's making me ask the following:

What if I wanted to write all my subclasses in a single .java file, and make them "all" placed outside the superclass to avoid talking about the nested classes?

What if I wanted to keep the superclass in a .java file and put "all" the subclasses into another .java file? I mean, superclass in a .java file, and all subclasses are in another .java file (all together) and make all these subclasses inherit from the superclass?

While writing this reply. I have tried (and thank you for your remark: what happened when you tried?) and the code seems to have no errors at all. The final is:

Re: Assigning a Value to a Class extending a Superclass

Usually, each class has its own file. When you create a new class, you create a new file. You're trying to shove all your classes into one file, which is giving you a headache.

You certainly can combine classes into a single file, if that actually fits your design. One way to do that is by nesting classes, where you have an inner class inside an outer class. This is useful when the inner class depends on something in the outer class, or when the inner class is only used in the outer class. The inner class can be static or non-static, based on the things I mentioned above.

Another way to do it, and what I think you're trying to do, is to simply list multiple classes inside a single file. The problem with that though, which the compiler is telling you about, is any classes other than the class for which the file is named must not be public. This is because the normal approach, with classes in different files, will not know where to find a class that is listed inside another class file!

Please split your classes up into multiple files, as I have no idea what you're trying to run.