• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

class confusion

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic