Re: JD1's Personal Development Blog
Quote:
I can't use the class1Object outside of the main method.
You are in Class1 and class1Object is actually the object of Class1 so, outside main() why do you need to use this object?
You can call or use any of instance variable or method of Class1 as far as you are working in Class1.
Well, if you still want to use object, you can make a method to accept object and pass the object into the method call.
Code :
public class Class1{
private int total;
private String name;
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static void main(String... args){
Class1 obj = new Class1();
obj.setTotal(1);
obj.setName("Mr.777");
usingObject(obj);
}
public static void usingObject(Class1 obj){
System.out.println(obj.getTotal());
System.out.println(obj.getName());
}
}
Re: JD1's Personal Development Blog
Quote:
Originally Posted by
Mr.777
You are in Class1 and class1Object is actually the object of Class1 so, outside main() why do you need to use this object?
You can call or use any of instance variable or method of Class1 as far as you are working in Class1.
Well, if you still want to use object, you can make a method to accept object and pass the object into the method call.
Code :
public class Class1{
private int total;
private String name;
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static void main(String... args){
Class1 obj = new Class1();
obj.setTotal(1);
obj.setName("Mr.777");
usingObject(obj);
}
public static void usingObject(Class1 obj){
System.out.println(obj.getTotal());
System.out.println(obj.getName());
}
}
Hi,
Some of that is still new to me. I still can't get my head around any of what you've written (since you're using alternative code). It's difficult for me to relate that back to my problem, let alone understand what it does. Also, how can I call other methods within the same class without using that class1Object. As you said, there's no point using an object to get into the same class, but how else do I access the other methods in that class?
Re: JD1's Personal Development Blog
1. You must know the difference between static and non static methods.
2. I will recommend you to write main() as a separate class instead of mixing it in the already written class. Because your program must be highly cohesive (independent) and loosely coupled (how a class knows about the other class)
I know you are a beginner so, just focus on your track and keep going. Good Luck.
Re: JD1's Personal Development Blog
Alright, I'll keep that in mind. Once again, I'd like to thank you for all your help today. I really appreciate it! :-*
Re: JD1's Personal Development Blog
Quote:
Originally Posted by
OA-OD-JD1
Alright, I'll keep that in mind. Once again, I'd like to thank you for all your help today. I really appreciate it! :-*
No need to say thanks. That's why, we are here at forums, to help everyone.
Re: JD1's Personal Development Blog
Quote:
You can use an array initialiser as follows. Data Type arrayName[] = {index1, index2, index3, etc};. That's a much easier was of going about building an array.
Though this method of declaring and creating an array is not wrong but the right convention is to use the syntax like;
Quote:
DataType[] arrayName;
These minor suggestions are just to improve your programming style as you are passing on with your concepts and topics. So, keep these in mind too.
Re: JD1's Personal Development Blog
Quote:
Originally Posted by
Mr.777
Though this method of declaring and creating an array is not wrong but the right convention is to use the syntax like;
These minor suggestions are just to improve your programming style as you are passing on with your concepts and topics. So, keep these in mind too.
And I'm glad you're providing these tips. This is the sort of thing I need. Also, with your example there, are you referring to an array initialiser with no indexes?
Re: JD1's Personal Development Blog
No, just declaring an array.
So, if you want to have a reference to an array you will most likely be;
Code :
int[] arrayTest;
arrayTest = new int[SIZE];
OR
Code :
int[] arrayTest = new int[SIZE];
Re: JD1's Personal Development Blog
Quote:
Originally Posted by
Mr.777
No, just declaring an array.
So, if you want to have a reference to an array you will most likely be;
Code :
int[] arrayTest;
arrayTest = new int[SIZE];
OR
Code :
int[] arrayTest = new int[SIZE];
Understood. Can you tell me why you put the square brackets after the data type instead of the array name?
Re: JD1's Personal Development Blog
Quote:
Originally Posted by
OA-OD-JD1
Understood. Can you tell me why you put the square brackets after the data type instead of the array name?
Well it's the convention from the Sun. Also, it is used to let others understand that we are going to have an array of that datatype named this. Like
The code will let others know easily that we are going to have an array of integers naming arrayName. Just a convention to follow by everyone so that everyone could code in a known standards and understand them easily though int arrayName[] is not wrong to write. It works similar to the latter.
Re: JD1's Personal Development Blog
Quote:
Originally Posted by
Mr.777
Well it's the convention from the Sun. Also, it is used to let others understand that we are going to have an array of that datatype named this. Like
The code will let others know easily that we are going to have an array of integers naming arrayName. Just a convention to follow by everyone so that everyone could code in a known standards and understand them easily though int arrayName[] is not wrong to write. It works similar to the latter.
Okay. In other languages, I've been used to doing it the way I orginally did, but since what you mentioned is standard Sun convention, I'll be sure to stick to that in the future.
Re: JD1's Personal Development Blog
You might want to check out the standard code conventions and the JLS, both of which are where we're getting most of these rules.
Code Conventions for the Java(TM) Programming Language: Contents
The Java Language Specification, Third Edition - TOC
Re: JD1's Personal Development Blog
Quote:
Originally Posted by
KevinWorkman
Thanks mate. I'll be sure to take a look some time.
Summing The Elements of An Array
Say we have an array with ten indices. Below is how we can calculate the sum of these indices.
Code java:
class Class1{
public static void main(String args[]){
int[] array1 = {1,2,3,4,5,6,7,8,9,10};
int total = 0;
for(int counter = 0; counter < array1.length; counter++){
total += array1[counter];
}
System.out.println("Total\t" + total);
}
}
Firstly, initialise your array. Then, create a variable to store the total. A simple For Loop set to iterate to the length of our array will do the trick. All you need to do is set the total to the total plus the array with the index if the current counter. A very simple concept.
In an unrelated topic, I'm now up to my 30th Java tutorial. I'm making some nice leadway here, though I've still got a heck of a lot to learn.
Re: JD1's Personal Development Blog
This blog has been continued over at Java Development Forums and in my JPF Blog.
Harvey's Java Blog
JD1's Personal Development Blog
Re: JD1's Personal Development Blog
Please be courteous enough to also link back to javaprogrammingforums.com from your own forums too.
Re: JD1's Personal Development Blog
Quote:
Originally Posted by
newbie
Please be courteous enough to also link back to javaprogrammingforums.com from your own forums too.
I've decided to continue the blog here (now that I've received blogging permissions).
Re: JD1's Personal Development Blog
i enjoy reading your blog OA-OD plus the help of super moderators
guys keep the fire burning :cool: