Results 1 to 6 of 6
  1. #1
    ron2794 is offline Member
    Join Date
    Dec 2010
    Posts
    38
    Rep Power
    0

    Default get my conecpts right , please !

    why the following things cannot be :

    1.why i cannot extend the non static variable from static method or something ?

    2.why i cannot use a variable directly in the class as :

    Java Code:
    class abc{
    int i=4;
    
    }
    class xyz extends abc{
    int j=7;
    i=9;
    /*this should be valid acc. to me bcz i think that when a class is extended then the
    variable are also inherited and now the class xyz has i and j both accesible so it should be able to change the variable value also. */
    }
    3. How i can make such a program that would continously generate objects and keep on extending classes automatically and gets up all the memory of the system until the system crashes.

    tell me also something more about these object concept tricks and tips...PLEZ

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Please at least let people know that you are cross-posting.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default

    Quote Originally Posted by ron2794 View Post
    why the following things cannot be :

    1.why i cannot extend the non static variable from static method or something ?
    Pardon? I didn't understand the question.

    Quote Originally Posted by ron2794 View Post
    2.why i cannot use a variable directly in the class as :

    Java Code:
    class abc{
    int i=4;
    
    }
    class xyz extends abc{
    int j=7;
    i=9;
    /*this should be valid acc. to me bcz i think that when a class is extended then the
    variable are also inherited and now the class xyz has i and j both accesible so it should be able to change the variable value also. */
    }
    If that's your code as written then that's a compiler error since:
    i=9;
    can only be in a method or code block.

    Quote Originally Posted by ron2794 View Post
    3. How i can make such a program that would continously generate objects and keep on extending classes automatically and gets up all the memory of the system until the system crashes.

    tell me also something more about these object concept tricks and tips...PLEZ
    Why on earth do you want to do 3? But using up all the memory in a system is a simple task of looping and adding "new Object()" to a List of some description. No need to extend classes.

  4. #4
    jomypgeorge is offline Member
    Join Date
    May 2010
    Posts
    90
    Rep Power
    0

    Default

    you can place it in a method

    public void method1()
    {
    i=9;
    }

    to run out memory just place an instance of same class inside it. it works recursively.
    class A
    {
    A objA = new A();
    }

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by jomypgeorge View Post
    to run out memory just place an instance of same class inside it. it works recursively.
    class A
    {
    A objA = new A();
    }
    No, that will cause a StackOverflow (unless the object is "large" enough and the recursion comes "late" enough) not an OutOfMemory. Not the same thing.

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by ron2794 View Post
    why the following things cannot be :

    1.why i cannot extend the non static variable from static method or something ?
    Because "non-static" is bound to the instance of a class and "static" is bound to the class itself. Two very different things.
    2.why i cannot use a variable directly in the class as :

    Java Code:
    class abc{
    int i=4;
    
    }
    class xyz extends abc{
    int j=7;
    i=9;
    /*this should be valid acc. to me bcz i think that when a class is extended then the
    variable are also inherited and now the class xyz has i and j both accesible so it should be able to change the variable value also. */
    }
    If the variable is declared to be private only that class has access to it, if it is declared default (i.e. no access modifier, as above) only that class or other classes in the same package can access it, if it is declared protected, that class, other classes in the same package, and subclasses have access to it, and with public all classes have access to it.
    3. How i can make such a program that would continously generate objects and keep on extending classes automatically and gets up all the memory of the system until the system crashes.

    tell me also something more about these object concept tricks and tips...PLEZ
    A while loop that creates objects and adds them to a list.

    Now, I hope that this homework is already past due and that you got the grade that you seemingly deserve.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •