Ask your own question, for FREE!
Computer Science 8 Online
OpenStudy (anonymous):

**MEDAL + FAN** Please check my Java Programming answers. If they're wrong, please help me figure out the answer (if you can/have the time). I want to learn how do these on my own.

OpenStudy (anonymous):

OpenStudy (woodrow73):

A keyword is something you type into the code itself -- a keyword has a special meaning in java -- examples of keywords are: int, float, double, switch, this, if, else, public, private When you instantiate a class, you use the word 'class' in it's header ``` public class HelloWorld{ } ``` A program can have multiple constructors -- with multiple constructors - you differentiate between them by the different parameters (data types in the parenthesis) of each constructor - I'll show an example. ``` public class CalledFromMain{ private int x; private boolean b; public CalledFromMain() { //no arg-constructor x = 0; b = false; } public CalledFromMain(int inputFromMain) { //this constructor accepts an int as an argument x = inputFromMain; b = false; } public CalledFromMain(int inputFromMain, boolean inputBoo) { x = inputFromMain; b = inputBoo; } //now I'll put in some 'accessor' methods which //access the field variables (x and b are the field variables of this class) public int getX() { return x; } public boolean getB() { return b; } } ``` Main class below vv I will create objects of the CalledFromMain class type, and will assign their field variables; using constructors -- and will read the values back using the methods. ``` public class Main { public static void main(String[] args) { CalledFromMain cfm = new CalledFromMain(); int x = cfm.getX(); boolean q = cfm.getB(); System.out.println(x + " ," + b + " are the field variables); CalledFromMain cfm2 = new CalledFromMain(52, true); System.out.println(cfm2.getX() + " ," + cfm2.getB() + " are the field vars"); } } ``` The console output will be 2 lines-- the first will show the field variables of cfm, the second will show the field variables of cfm2 ``` cfm: 0, false cfm2: 52, true ``` Note that constructors consist of 3 parts- you always start with public keyword, followed by the exact same name of the class that the constructor is in. The last part are the parameters (what you put in the parenthesis).

OpenStudy (woodrow73):

A reference variable (a variable that references an object) is unique, and easy to create... if I have a variable.. cfm let's say, and I want to create a reference variable to it- I simply have to do: ``` CalledFromMain cfm3 = cfm; ``` A reference variable has a few remarkable traits - one of them being, that the two variables "reference" the same object in memory -- this means that if I do.. ``` cfm3 = new CalledFromMain(555); System.out.println(cfm.getX()); ``` console will print: ``` 0 ``` so if you change a field on cfm3, it will then change the field on cfm.

OpenStudy (anonymous):

So after reading through that, I got: 1.) A 2.) A currently working on 3.

OpenStudy (anonymous):

3.) C ?

OpenStudy (woodrow73):

What constructors don't have is a return type -- note that in the constructor header ``` public CalledFromMain() ``` there is no return type.. like in a method, where the return type can be any type, or void to specify no return type. ``` public int getX(){ return x; //int represents that I'm returning a value of type int } ```

OpenStudy (woodrow73):

yup

OpenStudy (anonymous):

ahhh, so 2.) C then? My bad, I see now in your previous answer. I misread.

OpenStudy (woodrow73):

Right

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!