|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming | |
java-constructors-super-mE nEed HelP b4 I gO InSaNE!
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | |||||||||||||
|
nVidia Enthusiast
|
I am currently teaching myself java through the All in one for dummies book...
I try to make sure that I can use what I'm studying before I move on to the next chapter. But I've hit a road block considering my learning style and they were pretty vague on constructors. They only gave examples that wouldn't compile My questions are: 1. Is this the constructor ()? (if not what is it?) 2.What is with the default setting of the constructor?(they were vauge...) 3.HOW THE HECK DO YOU YOU USE SUPER WHEN THE MAIN METHOD IS STATIC?!?!?!?!?!?!?!?!?!?!?!?!?
__________________
"We are all prophets, that is why we make so many mistakes" "One who wears a mask wants to become someone else one who wears a mask somewhere other than the face wants to become many" "The truth is a lie" "There are only a few free people in the world, the insane"
|
|||||||||||||
|
|
|
|
|
#2 (permalink) | ||||||||||||||
|
Intel Overclocker
|
1.
2. If you are going to construct an object, you need to declare it as well as instantiate it with paramaters. Object name = new Object(parameters); Different objects require different parameters. look at the java api. Let's take a look at the StringTokenizer class. String coolio = "cool string"; StringTokenizer st = new StringTokenizer(coolio); the new StringTokenizer st is given a value of a new StringTokenizer that uses the string coolio to get it's value. what it does is break down the string into tokens. the default delimiters (or what it uses to break the string into tokens) are space and some other characters. so if want to use the object's method "nextToken" you can : String a = st.nextToken(); this will set the String "a" equal to the value of the first "token" which is "cool" and String b = st.nextToken(); will set b = "string";
__________________
Quote:
SLi Zone PSU listing- check to see if your PSU is SLi certified. BF2 stats BF2142 stats My Guide to BF2 Unlocks
|
||||||||||||||
|
|
|
|
#3 (permalink) | |||||||||||||
|
nVidia Enthusiast
|
1. Let me see if I understand, a constructor is only needed when you create a new object or new instance of an object?
__________________
"We are all prophets, that is why we make so many mistakes" "One who wears a mask wants to become someone else one who wears a mask somewhere other than the face wants to become many" "The truth is a lie" "There are only a few free people in the world, the insane"
|
|||||||||||||
|
|
|
|
|
#4 (permalink) | ||||||||||||
|
Programmer
|
Quote:
A constructor is used when you created a new object to predefine local values. for example if we have an object called 'System' our class can look as follows. public class System { private int ramAmount; private int cpuSpeed; //constructors public Sytem() { //this is the default constructor, does nothing } //assignment constructor public System (int newCpuSpeed, int newRamAmount) { ramAmount = newRamAmount; cpuSpeed = newCpuSpeed; } ...other class methods etc } so in another part of our application we can create a new object system with (a) the default constuctor or (b) the preassignment values. a) System myNewRig = new System(); b) System myNewRig = new System(3600, 2); - where 3600 is cpu speed, 2 is ram. You can have many constuctors to handle different cases. I hope this example helps, i find it easier to looks at example or actually do the work on your own.
__________________
Relax and enjoy life!
|
||||||||||||
|
|
|
|
|
#5 (permalink) | ||||||||||||||
|
nVidia Enthusiast
|
Quote:
![]()
__________________
"We are all prophets, that is why we make so many mistakes" "One who wears a mask wants to become someone else one who wears a mask somewhere other than the face wants to become many" "The truth is a lie" "There are only a few free people in the world, the insane"
|
||||||||||||||
|
|
|
|
|
#6 (permalink) | ||||||||||||
|
Programmer
|
Quote:
Sure, the 'super' call is relating to the objects parent or extending class. If the example i gave was like the following: public class System extends Server { private int ram; private int cpuSpeed; //constructor public System (int newRam, int newCpuSpeed) { super(false); // since this is a home PC we know is not a server... ram = newRam; cpuSpeed = newCpuSpeed; } } public class Server { private boolean isServer; //constructor public Server (boolean newIsServer) { isServer = newIsServer; } } Since System extends server, it also holds the values which are in the Server class. By using super, we can write those values which are not directly in our system class. As you progress you java development, you will find this is very useful especially when using swing frames and panels. Hope it helps, sorry for the bad formatting, but it's the forum text parser ![]()
__________________
Relax and enjoy life!
|
||||||||||||
|
|
|
|
|
#7 (permalink) | |||||||||||||
|
nVidia Enthusiast
|
could you redo that example with the static main method, what originally through me off was in NetBeans it said that I couldn't use super because it was non static and it referenced a static method. ty again!
__________________
"We are all prophets, that is why we make so many mistakes" "One who wears a mask wants to become someone else one who wears a mask somewhere other than the face wants to become many" "The truth is a lie" "There are only a few free people in the world, the insane"
|
|||||||||||||
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|