Welcome to the Java Programming Forums


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.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Assigning a Value to a Class extending a Superclass

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Assigning a Value to a Class extending a Superclass

    I'm learning Java programming and today I was into a document on classes.

    I've created a superclass and a subclass as the following:

    package objects;
    public class Objects {
     
        String carrier="Zain JO";
        String color="Black";
        int battery=1;
     
        public void ObjectsCon(int b){
            battery=b;
        }
     
        public class Android extends Objects{
     
            String company="Google";
            String model="Nexus";
            double screen=4.5;
     
            void AndroidCon(double s){
                screen=s;
            }
        }
     
        public static void main(String[] args) {
     
            Objects obj=new Objects();
            obj.ObjectsCon(2);
     
        }
    }

    As you see, I have assigned a value of "2" to the superclass, successfully. but my problem is How to assign a value to "s" through its method "AndroidCon"?

    I tried to:

    Android device=new Android();

    but it didn't work. I get: on-static variable this cannot be referenced from a static context
    Last edited by ubermoe; September 20th, 2011 at 03:48 PM. Reason: quoted a text as code, by mistake


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Assigning a Value to a Class extending a Superclass

    but it didn't work. I get: on-static variable this cannot be referenced from a static context
    You are attempting to access an inner class from a static context. Either define the Android class as static, or have the Android class as its own class (rather than an inner class). The following link provides a bit of reasoning as to why this is the case:
    Nested Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Assigning a Value to a Class extending a Superclass

    package objects;
    public class Objects {

    String carrier="Zain JO";
    String color="Black";
    int battery=1;

    public void ObjectsCon(int b){
    battery=b;
    }

    public class Android extends Objects{

    String company="Google";
    String model="Nexus";
    double screen=4.5;

    void AndroidCon(double s){
    screen=s;
    }
    }

    public static void main(String[] args) {

    Objects obj=new Objects();
    obj.ObjectsCon(2);
    Android android = obj.new Android();
    android.AndroidCon(3);

    }
    }


    This will work fine and this is the way we construct the object of inner class

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Assigning a Value to a Class extending a Superclass

    cs13, this is the third time I've sen you (poorly) spoon-feeding. Please read this before you post again: http://www.javaprogrammingforums.com...n-feeding.html

    Continued spoon-feeding will cause me to request that you are banned, for the reasons outlined in that article.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Thumbs up 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

    Thank you so much for helping me out solve the issue.

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.
    Last edited by ubermoe; September 21st, 2011 at 09:02 AM.

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Assigning a Value to a Class extending a Superclass

    Quote Originally Posted by ubermoe View Post
    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.

    Quote Originally Posted by ubermoe View Post
    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?

    Quote Originally Posted by ubermoe View Post
    - 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?

    Quote Originally Posted by ubermoe View Post
    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.

    Quote Originally Posted by ubermoe View Post
    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.
    No problem. I'm glad we could help.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Junior Member
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Smile Re: Assigning a Value to a Class extending a Superclass

    I'm not sure I understand the question. What happened when you tried? Are you perhaps talking about anonymous inner classes?
    The .java template inserted automatically from NetBeans is:

    package javatest;
     
    /**
     *
     * @author ubermoe
     */
    public class Javatest {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
        }
    }

    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:

    package objects;
    public class Objects [B]{[/B]
     
        String carrier="Zain JO";
        String color="Black";
        int battery=1;
     
        public void ObjectsCon(int b){
            battery=b;
        }
     
        static class Android extends Objects{
     
            String company="Google";
            String model="Nexus";
            double screen=4.5;
     
            void AndroidCon(double s){
                screen=s;
            }
     
            void AndroidPrint(){
     
                System.out.println(company+model+carrier+color+screen+battery);
            }
     
        }
     
        public static void main(String[] args) {
     
            Objects obj=new Objects();
            obj.ObjectsCon(2);
     
            Android device= new Android();
            device.AndroidCon(3.7);
            device.AndroidPrint();
     
        }
    [B]}[/B]

    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:

    package objects;
    public class Android extends Objects{
     
            String company="Google";
            String model="Nexus";
            double screen=4.5;
     
            void AndroidCon(double s){
                screen=s;
            }
     
            void AndroidPrint(){
     
                System.out.println(company+model+carrier+color+screen+battery);
            }
     
        }
     
    public class Objects {
     
        String carrier="Zain JO";
        String color="Black";
        int battery=1;
     
        public void ObjectsCon(int b){
            battery=b;
        }
     
        public static void main(String[] args) {
     
            Objects obj=new Objects();
            obj.ObjectsCon(2);
     
            Android device= new Android();
            device.AndroidCon(3.7);
            device.AndroidPrint();
     
        }
    }

    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:

    package objects;
    class Android extends Objects{
     
            String company="Google";
            String model="Nexus";
            String system="Gingerbread";
            double screen=4.5;
     
            void Android(String os){
                system=os;
            }
     
            void AndroidPrint(){
     
                System.out.println(company+model+carrier+color+screen+battery+system);
            }
        }
     
    class iPhone extends Objects{
     
            String company="Apple";
            String model="iPhone 4";
            String system="iOS";
            double screen=4.5;
     
     
            void iPhone(String os){
                system=os;
            }
     
            void iPhonePrint(){
     
                System.out.println(company+model+carrier+color+screen+battery+system);
            }
        }
     
    public class Objects {
     
        String carrier="Zain JO";
        String color="Black";
        int battery=1;
     
        public void ObjectsCon(int b){
            battery=b;
        }
     
        public static void main(String[] args) {
     
            Objects obj=new Objects();
            obj.ObjectsCon(2);
     
            Android device= new Android();
            device.Android("Android");
     
            iPhone ios= new iPhone();
            ios.iPhone("iOS");
     
            device.AndroidPrint();
            //System.out.println("\n");
            ios.iPhonePrint();
     
        }
    }

    but now, NetBeans is now showing me any outputs. It only says run:BUILD SUCCESSFUL (total time: 0 seconds)

    What's wrong now?

    Note: I'm running NetBeans v7.0.1 over Mac OS X Lion.

  10. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.

    Also, please see the tutorial on properly calling constructors: Providing Constructors for Your Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. The Following User Says Thank You to KevinWorkman For This Useful Post:

    ubermoe (September 22nd, 2011)

Similar Threads

  1. Problem with subclass and superclass methods
    By Farmer in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 15th, 2011, 08:43 PM
  2. class extending
    By Reem in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 18th, 2010, 01:49 AM
  3. Replies: 7
    Last Post: November 16th, 2010, 08:42 PM
  4. Instantiating a SuperClass Map from a SubClass
    By drexasaurus in forum Collections and Generics
    Replies: 1
    Last Post: September 9th, 2010, 10:51 PM
  5. adding get mothods to a class extending thread
    By aliaa2a in forum Object Oriented Programming
    Replies: 6
    Last Post: August 3rd, 2009, 06:41 AM

Tags for this Thread