File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes class confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "class confusion" Watch "class confusion" New topic
Author

class confusion

tim huntington
Greenhorn

Joined: Aug 26, 2004
Posts: 4
coming from basic i am confused by the way events in a class happen.for example...

class whatHappend {
int i = 1;
int j = 2;
void f() {
int i = 3
int j = 4
}
}

now when i view that i think of how it would work in basic,i and j are both given a number and f() then runs.but what i think happens in java is when that class is called,nothing happens,is that correct?it becomes like vb in a way i think,you need to say whatHappend.* in order to get the line of code you want to work correct?so if i called whatHappend.i then i would become 1?
[ August 26, 2004: Message edited by: tim huntington ]
omar bili
Ranch Hand

Joined: Aug 13, 2004
Posts: 177
class whatHappend {
int i = 1;
int j = 2;
void f() {
int i = 3 // this i is != than then i in the class it is a local variable in
// then function f
int j = 4// the same for j
}
}
this class has a default constructor
so

whatHappend what = new whatHappend () ;
System.out.println(""+what.i) ; // 1
System.out.println(""+what.j) ; // 2

f() ;

System.out.println(""+what.i) ; // 1 nothing changes
System.out.println(""+what.j) ; // 2
to modify the value of i and j just change the function f to :

void f() {
i = 3 ;
j = 4 ;
}
hope i helped.
sever oon
Ranch Hand

Joined: Feb 08, 2004
Posts: 268
Your take is kind of right but kind of wrong. In Basic, statements are executed in the sequence in which they appear...that is, everything is executed "in time". In Java, objects exist "in space". Methods of a particular object do not run until called.

So, let me mix it up on you a bit to help you understand with your own example.



In the above example, the assignments of 3 and 4 to variables i and j occur before foo() could ever run, even though they appear below the statements in foo(). In fact, the Java compiler expands the above code to the following:



The order in which the statements appear within the class is insignificant. The scope of those statements within the class is not (i.e., whether a statement appears within the scope of a method, a constructor, etc). For instance, assignments to instance variables i and j occur in the constructor, which is called when a new instance of class WhatHappened is created. The variables x and y are local to the method foo()--in other words, they don't even exist until some other bit of code in the application creates a WhatHappened object and calls its foo() method. Here's an example of that happening:



So you see, in Java, when you implement a class, the code in that class is not necessarily going to run at all. It completely depends on whether some other bit of code creates an instance of that class (in which case the specified constructor runs) and calls its methods (in which case those methods will run).

sev
[ August 26, 2004: Message edited by: sever oon ]
tim huntington
Greenhorn

Joined: Aug 26, 2004
Posts: 4
how do you pick which are things automatically done when the class is called and which happen only when they are called by using the class name then the dot and the object you want to call.like say i want a class called Alpha,and when Alpha is called i want it to i want it to set a,b,c as int,run beta(),and make a object that can be called from the class,like Alpha.one().

class whatHappend {
// how do you tell one to run when, whatHappend what = new whatHappend()
// happens,and one that will only work when whatHappend.*() is called
// without placing one in the constructor
void h() {
int i = 1;
int j = 2;
}
void f() {
int i = 3
int j = 4
}
}
[ August 26, 2004: Message edited by: tim huntington ]
Jason Fox
Ranch Hand

Joined: Jan 22, 2004
Posts: 114
I'm not sure what you mean by automatically, but ClassName.Method refers to static methods. There are two ideas here, both interrelated. You have a Class, which is sorta like a blueprint, and then you have an Object, which is the actual thing (Floorplans vs. the actual building) static members are methods and variables that are associated with the Class, not the Object. So, you do not need to instantiate the Object, just call the method directly, using ClassName.MethodName (or ClassName.VariableName for a variable). For all other methods and variables, you must create an Instance of the Class (usually using the new operator), and then use that instance. Example:


I hope that clears up some of your questions
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

Java is not like basic... the code is not executed line by line. in your original post, NO SINGLE LINE OF YOUR CODE would be executed, until somebody created a whatHappend object. i could write, in MY code


at this point, i would have a reference to a hunk of memory that has a whatHappened object. the "new whatHappend()" would cause a spot in memory to be allocated, with enough room to hold the two variables. also, if this is the first time anybody has created on of these, a special place in memory is reserved for the METHODS of the class...

in other words, if i create 300 objects of this type, there are 300 sets of the i and j in memory. but the methods are only stored once.

so when does the f() method run? at any time in my code where i do something like:

myVariable.f();

NOW that code will run. due to other reasons we probably don't need to go into here, how you have this written, the i and j that my object are holding will not change, but that's another thread topic.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: class confusion
 
Similar Threads
Nested Control Structures
Passing an array to a method
"Non-static method cannot be referenced from a static context" ?
initialization sequence
Static Variables Compilation