Results 1 to 3 of 3
- 08-18-2012, 09:25 AM #1
Member
- Join Date
- Aug 2012
- Posts
- 10
- Rep Power
- 0
needed clarificarions on constructors
Hi am learning JAVA . in the below example
1.how exactly the two samples differs please clarify
Class A {
int i=10;
}
and
Class A {
int i=10;
A(intj);//Initializing the constructor
}
2. Whats the necessity of initializing the constructor when u are defining the variable .
- 08-18-2012, 11:18 AM #2
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
Re: needed clarificarions on constructors
1. You second example is not correct, you might want something like this:
When you creating a class and doesn't provide a constructor it will have a default constructor. A constructor that doesn't have any parameter. A constructor is used to create an instance of corresponding class. The difference between your first and second example that your second example will not have the default constructor because you provided a constructor that take a single parameter. To instantiate your first class you can do it like A a = new A(); but the second A class can only be instantiate by A a = new A(0); You can't instantiate it using the default constructor.Java Code:class A { int i = 10; A(int j) { // ... } }
2. You use constructor to instantiate or initialize objects or variables in your class. This is to make sure that it is in the state that you want when you class it running it function.Website: Learn Java by Examples
- 08-18-2012, 01:00 PM #3
Member
- Join Date
- Aug 2012
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
Constructors
By cups in forum New To JavaReplies: 1Last Post: 02-15-2012, 11:55 AM -
Constructors
By EdOBannon in forum Advanced JavaReplies: 2Last Post: 12-15-2011, 11:05 AM -
Need help with constructors
By tpfaff in forum New To JavaReplies: 10Last Post: 10-22-2010, 04:33 AM -
constructors?
By shroomiin in forum New To JavaReplies: 4Last Post: 10-13-2009, 02:14 PM -
constructors
By khamuruddeen in forum New To JavaReplies: 2Last Post: 12-01-2007, 03:15 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks