At some point, I realized that the closest thing Java had to what C calls a structure, is a Class with data elements defined within the class.
Prior to that I had been using Classes like C uses libraries, and they had only methods (which I have always called functions).
The functions returned a string array, for handling the multiple elements I needed to return. (I had to do a lot of converting between String and other data types).
Then I realized that putting the data fields inside the Class was cleaner.
So now I have this:
public static Task doSomethingToTask(Task taskIn)
Task taskOut = new Task();
do stuff to taskIn that results in any changes to the task all ending up in taskOut;
The call to the method is like this
mytask = Task.doSomethingToTask(mytask);
OR this works as well
mytask = mytask.doSomethingToTask(mytask);
However, when using classes, that are provided, if I act on an instance of the class, I don't have to explicitly pass the instance in.
I should be able to just do this:
mytask = mytask.doSoemthingToTask();
But I have no idea how to write the method, so it knows what to doSomethingToTask to.
Are you doing a course? Have you been taught about objects and classes in Java? Have you got a book which teaches about objects? Some books (eg Deitel, Head First) definitely start teaching about objects in their earlier chapters. You can find more about books here.
Classes and objects are very different from structs.
Joined: May 04, 2009
It kind of told me that when it told me I could not use this (when I tried to use this) with static.
No Cambell, I am not taking a class, I am winging it, and have a very short time line to create an interface, in Java, to an old C application.
Originally (when I agreed to do this) I was going to use C and a CGI script.
I thought I would pick up Java much faster, based on my experience with other languages. But it is not just a syntax switch, it is a paradigm shift.
However, I still am seeing a lot of similarities between a C data structure, combined with a library of functions to act on that data structure, and a java class.
Is there something in java, that is a closer match to a C data structure?
What OO defines as a class, seems like the very paradigm for which I would normally combine data elements into a structure for, when coding in C (or elements into the same table, when setting up a database).
A Java "struct" would be a class with only properties. There would then be one or more static utility classes that took instances of the "struct" class and operated on them, like so:Etc. In this (admittedly unhelpful) example, the Java version would look more like:(With the caveat that most Java folks will tell you that the "x" property should be private and have a getter and setter, and disallow direct access as shown here in the interests of brevity.) You can't have "naked" functions in Java; they have to be encapsulated in a class.
Personally, I don't see that classes/objects are *very* different from structs. IMO the biggest differences are encapsulation and inheritance. C structs can be used to implement OO (one vision of it), Java kicks it up a notch and leaves a lot of the work to the compiler and VM.
Joined: Oct 13, 2005
A struct is intended to contain data only. A class is intended to encapsulate data and behaviour. Yes, you can mimic object-orientation by passing a pointer to the struct as an argument to the method, but object-orientation should involve the methods of the class acting on the data in the class. So I still think structs and (ordinary) classes are very different. I think David Newton's FooOps class shows that distinction well.
Agreed there are instances where there is no need for the class to hold any data. For example, the Math class works out the square root; to do so it receives a number and returns a number. If you ask for Math.sqrt(2.0) it gives you 1.414.... and has no need to retain the 2.0 nor the 1.414.... That is where you call the methods static and it works as a plain simple "function".
That's one way to "mimic" OO in C, I guess, but there are way better ones--there are many fun, mind-bending papers on the topic.
Joined: May 04, 2009
Thank you for the analysis
I was just wanting to get the benefits of a struct (for data grouping) in java, and a class worked very well.
For those where it made sense, to also add methods (because I was going to do things to instances of the class) I added methods.
Otherwise I did not.
I did not end up using inheritance, for any of my custom classes. However, based on the nature of my data;
in some cases, it would have made complete sense to do this.
No time for re-design right now.
I still tend to think like an old procedural language coder, having a background in old procedural languages.
The first module I wrote, is so obviously done by someone who had little java knowledge, and was trying to adapt java syntax to old procedural language concepts. But I got better as I progressed.
subject: calling a method in a class with an instance of the class?