Developer community
0 votes
98 views
asked by Sr Member (4.8k points) 10 24 31 | 98 views

1 Answer

0 votes
Every class must have at least one constructor.
If there is no constructors for your class, the compiler will supply a default constructor(no-arg constructor).
A constructor is used to construct an object.
A constructor looks like a method and is sometimes called a constructor method.
A constructor never returns a value
A constructor always has the same name as the class.
A constructor may have zero argument, in which case it is called a no-argument (or no-arg, for short) constructor.
Constructor arguments can be used to initialize the fields in the object.
The syntax for a constructor is as follows.
 
constructorName (listOfArguments) {
    [constructor body]
}
public class MainClass {
  double radius;
 
  // Class constructor
  MainClass(double theRadius) {
    radius = theRadius;
  }
}
 
answered by Geek (15.3k points) 17 41 61

Related questions

0 votes
1 answer
asked by sara Guru (24.6k points) 29 51 79 | 16k views
0 votes
1 answer
asked by smith Geek (15.3k points) 17 41 61 | 90 views
0 votes
1 answer
asked by felin Advisor (8.4k points) 10 15 18 | 1.4k views
+1 vote
1 answer
asked by augustaina Sr Member (4.8k points) 10 24 31 | 4.3k views
0 votes
1 answer
asked by augustaina Sr Member (4.8k points) 10 24 31 | 74 views
Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

Categories


C Books Guide and List
C++ Books Guide and List
Best Java Books

3.8k questions

4k answers

117 comments

10.7k users

3,751 questions
4,046 answers
117 comments
10,709 users