Results 1 to 3 of 3
  1. #1
    silvia is offline Member
    Join Date
    Jul 2007
    Posts
    35
    Rep Power
    0

    Default I have a problem with variable "i"

    I have a problem. Why does the program ask me for the variable "i" three times, and how could I fix it?
    Sorry for making it so long, but I don't know any other way of illustrating my problem..

    Java Code:
    import java.util.*;
        public class Test
       {
           public static void main (String args[])
          {
             Testing1 T;
             T = new Testing1();
             T.Testing1Method();
          
             Testing2 T2;
             T2 = new Testing2();
             T2.Testing2Method();
          
             Testing3 T3;
             T3 = new Testing3();
             T3.Testing3Method();
          }
       }
    Java Code:
    //Testing1.java
    
       import java.util.*;
        public class Testing1
       {
          Scanner scan = new Scanner(System.in);
          int i = scan.nextInt();
       
           public void Testing1Method()
          {
             System.out.println("Test1= " + i);
             Testing2Method();
          }
           public void Testing2Method()
          {
          }
       }
    Java Code:
    //Testing2.java
     
        public class Testing2 extends Testing1{
           public void Testing2Method(){
             super.Testing2Method();
             System.out.println("Test2 " + i);
             Testing3Method();
          }
           public void Testing3Method()
          {
          }
       }
    Java Code:
    //testing3.java
    
        public class Testing3 extends Testing2
       {
           public void Testing3Method()
          {
             super.Testing3Method();
             System.out.println("Test3 " + i);
          }
       }
    Thanks

  2. #2
    cruxblack is offline Senior Member
    Join Date
    Jul 2007
    Posts
    130
    Rep Power
    0

    Default

    For the problem of why the program ask u for the variable i 3 times, it involves the logical steps the compiler take to compile ur code
    Java Code:
             Testing1 T;
             T = new Testing1();//create an object of testing1
             T.Testing1Method();
          
             Testing2 T2;
             T2 = new Testing2();//create an object of testing2
             T2.Testing2Method();
          
             Testing3 T3;
             T3 = new Testing3();//create an object of testing3
             T3.Testing3Method();
    U can see that in each class, u didn't include a constructor, so the compiler creates a default constructor for u during the compilation process, usually the default constructor won't do anything, but in this case, somehow it created a default constructor that consists of something like this
    Java Code:
    public Testing1//the Testing1 default constructor
    {
          Scanner scan = new Scanner(System.in);
          int i = scan.nextInt();//it'll ask for an input
    }
    Testing3 are inherited from Testing2 which were inherited from Testing1
    Each time u create an object from a class that was inherited from another class (..lets use Testing2 for example..),
    if u don't manually include and write a constructor for ur inherited class (..which is Testing2..), the compiler won't go straight into making u a default constructor for the inherited class, but instead, it will look into the parent class 1st (..Testing1 in this case..) and check if the parent class has a constructor that it can use as the constructor of the inherited class (..Testing2..), if the compiler found no available constructor after searching all of the available parent classes, then it'll create a default constructor.

    If i'm not mistaken, u created 3 objects, each was an instance of Testing1, Testing2 and Testing3. But since there's no constructor for each classes, the compiler generate a same default constructor to create each object, which was the default constructor for Testing1 that i just wrote above, which as u know, asked for an input, thats why it asked for the 'i' variable 3 times :D

    If i may ask, what was the code supposed to do actually?:confused:
    Im not really sure, but judging from the source code u just written, i presume u r trying to do some examples about either polymorphism or overriding/overloading methods with an output of this sort maybe?
    Java Code:
    <type an input for the variable 'i' here, ex : 5>
    Test1 = 5
    Test2 = 5
    Test3 = 5
    <another input for 'i', ex : 4>
    Test2 = 4
    Test3 = 4
    <3rd input for 'i', ex : 6>
    Test3 = 6
    :confused:

  3. #3
    igorgf is offline Member
    Join Date
    Aug 2007
    Posts
    2
    Rep Power
    0

    Exclamation

    All the problem is in your access modifier for 'i';

    It is so called Default - visible only from within file where class is defined and not accessible for others. If you came from VB - this could be the case that you would assume that default access modifier should be Public if you don't give one... But it is not.... Change to Public or Protected and it will work just fine.

Similar Threads

  1. Hwlp with "Open", "Save", "Save as..."
    By trill in forum New To Java
    Replies: 3
    Last Post: 11-02-2010, 09:26 AM
  2. "Displayed tab width" problem...
    By Petike in forum Eclipse
    Replies: 0
    Last Post: 03-17-2008, 09:39 PM
  3. Replies: 1
    Last Post: 07-27-2007, 10:59 PM
  4. Replies: 1
    Last Post: 07-25-2007, 10:55 PM
  5. Problem with "GregorianCalendar"
    By tola.ch2004 in forum New To Java
    Replies: 2
    Last Post: 07-12-2007, 08:12 PM

Posting Permissions

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