Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-18-2007, 06:11 PM
Member
 
Join Date: Jul 2007
Posts: 35
silvia is on a distinguished road
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..

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(); } }
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() { } }
Code:
//Testing2.java public class Testing2 extends Testing1{ public void Testing2Method(){ super.Testing2Method(); System.out.println("Test2 " + i); Testing3Method(); } public void Testing3Method() { } }
Code:
//testing3.java public class Testing3 extends Testing2 { public void Testing3Method() { super.Testing3Method(); System.out.println("Test3 " + i); } }
Thanks
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-20-2007, 07:43 AM
Senior Member
 
Join Date: Jul 2007
Posts: 130
cruxblack will become famous soon enough
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
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
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

If i may ask, what was the code supposed to do actually?
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?
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
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-08-2007, 12:05 AM
Member
 
Join Date: Aug 2007
Posts: 2
igorgf is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Displayed tab width" problem... Petike Eclipse 0 03-17-2008 10:39 PM
Hwlp with "Open", "Save", "Save as..." trill New To Java 1 07-31-2007 08:53 AM
Exception in thread "main" java.net.ConnectException: Connection timed out osval Advanced Java 1 07-27-2007 11:59 PM
Error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException romina New To Java 1 07-25-2007 11:55 PM
Problem with "GregorianCalendar" tola.ch2004 New To Java 2 07-12-2007 09:12 PM


All times are GMT +3. The time now is 10:38 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org