Quantcast

Maximum PC

It is currently Wed Apr 23, 2014 6:53 pm

All times are UTC - 8 hours




Post new topic Reply to topic  [ 12 posts ] 
Author Message
 Post subject: Procedures in Java
PostPosted: Thu Aug 20, 2009 12:17 pm 
Northwood
Northwood
User avatar

Joined: Mon Sep 17, 2007 1:27 pm
Posts: 3311
Location: Toronto, ON
I've tried googling it, I can't seem to find any relevant results.

How do I create procedures in Java? Basically I want to name a block of code and be able to call upon and execute that code when needed.


Top
  Profile  
 
 Post subject:
PostPosted: Thu Aug 20, 2009 12:37 pm 
Team Member
Team Member

Joined: Sat Dec 22, 2007 6:52 pm
Posts: 202
I can only think of creating a new object and placing the aforementioned code in that object. Then refer to the object you just created when you want to run that code. That's how I learned it anyway. Sorry if that's not really anything like want you want to do.


Top
  Profile  
 
 Post subject:
PostPosted: Thu Aug 20, 2009 8:34 pm 
Million Club - 5 Plus*
Million Club - 5 Plus*
User avatar

Joined: Sun Sep 12, 2004 6:37 pm
Posts: 4740
Location: In the monkey's litterbox
You're looking for functions. Don't worry that they're all marked public static for now, as that will become more relevant when you learn more.

Code:
public class SimpleProgram {
    public static void Main(String[] args) {
        System.out.println("Hello from main");
        otherFunction();
    }

    public static void otherFunction() {
        System.out.println("Hello from otherFunction()");
    }
}


Top
  Profile  
 
 Post subject:
PostPosted: Fri Aug 21, 2009 7:40 am 
Northwood
Northwood
User avatar

Joined: Mon Sep 17, 2007 1:27 pm
Posts: 3311
Location: Toronto, ON
OK, thanks that worked, but I have another question, is there a way of declaring a variable, and having that variable be able to be accessed anyplace in the program? Even if its in a different function?


Top
  Profile  
 
 Post subject:
PostPosted: Fri Aug 21, 2009 8:10 am 
Super Mario Banhammer
Super Mario Banhammer

Joined: Fri Aug 25, 2006 11:20 am
Posts: 595
guoruiwu1994 wrote:
OK, thanks that worked, but I have another question, is there a way of declaring a variable, and having that variable be able to be accessed anyplace in the program? Even if its in a different function?


Declare the variable as static inside a class:

Code:
public class GlobalDataStore
{

     public static long globalData = 0;

}

// you can now access this variable from other classes like so: 
// GlobalDataStore.globalData  There will only be one value for globalData
// as it is declared static


Top
  Profile  
 
 Post subject:
PostPosted: Sat Aug 22, 2009 12:40 pm 
Northwood
Northwood
User avatar

Joined: Mon Sep 17, 2007 1:27 pm
Posts: 3311
Location: Toronto, ON
MaxFan wrote:
guoruiwu1994 wrote:
OK, thanks that worked, but I have another question, is there a way of declaring a variable, and having that variable be able to be accessed anyplace in the program? Even if its in a different function?


Declare the variable as static inside a class:

Code:
public class GlobalDataStore
{

     public static long globalData = 0;

}

// you can now access this variable from other classes like so: 
// GlobalDataStore.globalData  There will only be one value for globalData
// as it is declared static


If I do that, it says: This static variable declaration is invalid, because it is not final, but is enclosed in an inner class.


Top
  Profile  
 
 Post subject:
PostPosted: Sat Aug 22, 2009 9:54 pm 
Million Club - 5 Plus*
Million Club - 5 Plus*
User avatar

Joined: Sun Sep 12, 2004 6:37 pm
Posts: 4740
Location: In the monkey's litterbox
guoruiwu1994 wrote:
MaxFan wrote:
guoruiwu1994 wrote:
OK, thanks that worked, but I have another question, is there a way of declaring a variable, and having that variable be able to be accessed anyplace in the program? Even if its in a different function?


Declare the variable as static inside a class:

Code:
public class GlobalDataStore
{

     public static long globalData = 0;

}

// you can now access this variable from other classes like so: 
// GlobalDataStore.globalData  There will only be one value for globalData
// as it is declared static


If I do that, it says: This static variable declaration is invalid, because it is not final, but is enclosed in an inner class.


Declare the variable at the same level as the functions, inside of that same class instead of nesting the classes.


Top
  Profile  
 
 Post subject:
PostPosted: Wed Nov 11, 2009 1:34 am 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
smartcat99s wrote:
You're looking for functions. Don't worry that they're all marked public static for now, as that will become more relevant when you learn more.

Code:
    public static void otherFunction() {
        System.out.println("Hello from otherFunction()");
    }
}


Actually, otherFunction is a procedure, not a function. Functions return values, procudes work based on side-effects... come on, you're not old enough to be forgetting this stuff yet Smartcat! ;-)

This is the biggest problem using Java as a teaching language. =\


Top
  Profile  
 
 Post subject:
PostPosted: Mon Jan 18, 2010 7:03 pm 
8086
8086

Joined: Tue Dec 15, 2009 11:27 am
Posts: 47
guoruiwu1994 wrote:
OK, thanks that worked, but I have another question, is there a way of declaring a variable, and having that variable be able to be accessed anyplace in the program? Even if its in a different function?

It looks like you're trying to use an object oriented language to write a non-object oriented program. There are a few instances where static functions and variables make sense, but their use should be more the exception than the rule. You may want to re-think how you're approaching the problem...or pick a language that's more suited to how you logically break down problems into algorithms.


Top
  Profile  
 
 Post subject:
PostPosted: Fri Jan 22, 2010 3:56 pm 
Northwood
Northwood
User avatar

Joined: Sun Jul 15, 2007 6:37 pm
Posts: 2261
Location: No. 1 Thread Killer
I started in java and still have problems with oop. then again i can barely program. either way.


Top
  Profile  
 
 Post subject:
PostPosted: Mon Mar 01, 2010 11:36 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
Dwood15 wrote:
I started in java and still have problems with oop. then again i can barely program. either way.


Dwood15 -- Java is actually a pretty damn good language in which to learn OOP. I would highly recommend that you spend some time with Bruce Eckel's Thinking in Java book -- he does a very good job of explaining the concepts w/o screwing things up with unnecessary crud (like using an applet to explain some OOP concept).

I listed it in the starting out thread or google it.


Top
  Profile  
 
 Post subject:
PostPosted: Mon Mar 01, 2010 11:45 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
TF_Titan wrote:
There are a few instances where static functions and variables make sense, but their use should be more the exception than the rule.


I disagree. There are plenty of instances where static methods are the correct choice. The use of static methods and static variables depends entirely on what you are doing. Would rewrite java.lang.Math? Making everything static is surely a sign of a bad design, but it sounds like you're tilting pretty far in the opposite direction which is also a mistake.


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group