Posted: Sat Dec 24, 2011 12:48 am Post subject: RE:Syntax Problems
Okay thank you, Tony.
About static and instance variables/methods, is this valid:
I have a Dog class and I make its Breathe() method static because all dogs will breathe the same (lets say). And I make a run() method an instance method because all dogs run at different speeds.
To my understanding, Breathe() would be defined as soon as it was written, whereas run() would have to be defined within another object . So, then, to call Breathe() I'd say ClassName.Breathe(), and to call run() I'd call ObjectName.run(), but I can't say ClassName.run() or ObjectName.Breathe(). Is this all correct? Because this is my current understanding.
Also, why would I want a static method? Why would I need to call ClassName.breathe() if I didn't have an object of the class?
DemonWasp wrote:
You cannot override a static method. You can override instance methods. You can overload both.
How would I override a method? When would I want to, really? If my run() method went off a speed instance variable, what would be the purpose of making run() an instance method? And finally, what does it mean overload both? I'm not really sure of what that would be referring to.
Thanks in advanced
EDIT: Is the point of a static variable that I can edit it for all the objects, both current and future, at once? (I just did this). But if I can't do that for a method, I don't see the point.
EDIT2: I changed the static variable by making a method to do so. I have a 'BankAccount' class, with a static variable Interest. And I have a method to change Interest.
Tony
Posted: Sat Dec 24, 2011 1:17 am Post subject: RE:Syntax Problems
Think of it this way: you've started out with Procedural programming, mixing code and data one line after another. Everything was flowing in one direction, so it sort of worked.
At some point you've discovered Types/records. Woah, whole new way to group and organize data together! A single array of points, instead of separate Xs and Ys that must be kept in sync.
Object Orientation takes it a step further. Now instead of grouping together and organizing data, you also get to group together and organize code!
Woah, what? Yup. A particular function can actually belong to the concept of that same "point" type, and be usable only by points. Previously functions had global scopes, and were available for use from the entire project in unexpected ways.
You want static methods when the code has to do with the entire class. A good example would be a method that returns the total number of dog instances created so far. This is a property of the class, not of an instance. It still makes sense to call Dog.count_of_dogs() and have it return 0.
Breathe() on the other hand should be an instance method as, conceptually, such an action is a property of an individual dog (an instance). You want to be able to say: Fido.breath(). It wouldn't make much sense to tell "the concept of a Dog" (what a class is) to breath.
Posted: Sat Dec 24, 2011 1:44 am Post subject: RE:Syntax Problems
I see, that makes much more sense. The Dog.count_of_dogs() example really clears it up.
But I'm still curious of the two things Demon Wasp said.
Aange10 wrote:
... How would I override a method? When would I want to, really? ... ... And finally, what does it mean overload both? I'm not really sure of what that would be referring to.
Thanks xD.
OT: I always question everything. Even in math class, or anything along those lines, I always want to know how things work. I don't like the concept of "this works, this is how you use it, go." I always want to know in what ways I can manipulate it, and why it does what it does (and usually how)...
Tony
Posted: Sat Dec 24, 2011 2:05 am Post subject: RE:Syntax Problems
Sure.
You override a method when you extend another class, and re-implement a method that the parent had already defined. E.g.
code:
class Dog {
String run() {
return "swift like the wind!";
}
}
But now you want to specialize. You have an older dog that's mostly the same as a regular dog, but doesn't run as fast. Darn.
code:
class OldDog extends Dog {
String run() {
return "kind of slow...";
}
}
(If you want to make sure that a method is not overriden, you can throw in a final keyword)
An overload is for differently typed arguments:
code:
class Dog {
String run(int speed) {
return Integer.toString(speed) + " units per whatever";
}
String run(String description) {
return description;
}
}
Posted: Sun Dec 25, 2011 1:31 pm Post subject: Re: RE:Syntax Problems
2goto1 @ 21/12/2011, 6:53 am wrote:
Class file names are simply conventions. The source code file names do not define your Java types. If you really wanted to for fun and giggles, you could place your Dog class definition inside a Cat.java source file
How so? This provides an error: "Public class Dog must be defined in its own file".
Tony
Posted: Sun Dec 25, 2011 2:23 pm Post subject: RE:Syntax Problems
When packages are stored in a file system (?7.2.1), the host system may choose to enforce the restriction that it is a compile-time error if a type is not found in a file under a name composed of the type name plus an extension (such as .java or .jav) if either of the following is true:
- The type is referred to by code in other compilation units of the package in which the type is declared.
- The type is declared public (and therefore is potentially accessible from code in other packages).
So matching filenames are enforced by the specification, with the exception of private class declarations that are not referred to by any other compilation units.
Meaning that you could have a private Dog class inside of Cat.java... but unless there's a public Cat class, Dog's code is not reachable.
Posted: Mon Dec 26, 2011 10:31 am Post subject: RE:Syntax Problems
The specification allows compilers to opt into the rule if they wish (...may choose...)
Sponsor Sponsor
Tony
Posted: Mon Dec 26, 2011 2:09 pm Post subject: RE:Syntax Problems
sure, you could probably find some compiler that doesn't follow some spec, but if you expect other people to compile your code without jumping through the hoops of installing and running an esoteric compiler... you should probably stick to the standard Java, as defined by the spec. That includes filenames. E.g. javac
Quote:
tony$ javac Foo.java
Foo.java:1: class Boo is public, should be declared in a file named Boo.java
public class Boo {
Constructors may contain code that is run when the object is created. It is sort of like setup code that you want done so the object is ready for what it's supposed to do.
Here's an example of what I'm talking about:
[[[[[CODE HERE]]]]]
You no longer HAVE to create an object and then fill the initial data with setters. You can skip the setters initially by using the constructor to set your data up. This doesn't mean setters are useless, because your data can change. However, this is just a way of making sure you don't create objects such as dogs initially with no legs. That's just an example of course.
The [[Code Here]] thing, has this code:
Java:
package society;
publicclass Human (){ private string Gender;
public Human (){
Gender = "Male";
} }
Upon reading this, the first sentance really struck me;
"Constructors may contain code that is run when the object is created."
I tested this theory, and when I have code (Say a class, with an instance age and a method looking like the above defining Age as 1), the code does not 'Run when the Object is Created".
I tested this by creating a new object of Dog and outputing its age. However it didn't output one, meaning that the code was NOT run when i created the new object.
Am I doing it wrong, am I misunderstanding, or is this passage inaccurate?
DemonWasp
Posted: Sat Jan 07, 2012 12:35 am Post subject: RE:Syntax Problems
You should probably post your code. You probably made a minor mistake (such as the 3 in the example you copy-pasted). A constructor must have the same name as the class, must not list any return type (not even void) and should probably be public (at least, for now: there are plenty of reasons for non-public constructors, but let's not get into that).
Java:
public class Dog {
private int age;
public Dog() {
System.out.println ( "no-argument constructor" );
}
public Dog ( int dogAge ) {
this.age = dogAge;
System.out.println ( "one-argument constructor: dog has age " + age );
}
public void Dog ( String name ) {
// not actually a constructor because of the "void" return type
}
public static void main ( String[] args ) {
new Dog(); // no-arg constructor
new Dog(3); // one-arg constructor
new Dog("Rex"); // compile error, because there is no constructor for Dog that takes only one String as an argument
}
}