• Post Reply Bookmark Topic Watch Topic
  • New Topic

some questions on this video by dereck banas  RSS feed

 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys

attached a screen shot of a video im watching on methods by dereck banas

just a few questions

why does he add:
MyPi = MyPi + 3.0; ...i dont understand why he adds 3.0 to overwrite the class variable with a local one

and why does he return 1 at the bottom? does a static method have to return something...and 1 is just a value that will never be used?

dbvid1.jpg
[Thumbnail for dbvid1.jpg]
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh,almost forgot...is there a specific reason his method is static? still not clear on when to use static variables/methods

 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok so i understand if the method isnt static you cant see the addNumbers method in the main method to print it out...but when would you use static methods? anytime you want something
program wide?
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't had enough time to read that program properly, but it looks like bad design.
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh thats very interesting campbell

these videos have been recommended to me by a lot of people,they are on youtube
i have a whole set...i should really go to college...lol
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Perhaps the reasons are ... that it is trying to teach you the features?  At this very early period, in your training, you are probably still seeing more skeleton code, than examples that compile and do something useful.

Henry
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This looks like it's trying to show you the difference between types of variables, and nothing more than that.
I wouldn't analyse why he's doing the actual lines as they are (as Henry says) just skeleton code to show concepts.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja wrote:and why does he return 1 at the bottom? does a static method have to return something...and 1 is just a value that will never be used?

Whether a method is static or not does not have anything to do with whether it returns a value or not. Static methods can also be void methods.
 
Ray mann
Ranch Hand
Posts: 48
Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The keyword Static simply means that the static Variable is the same to all objects.

what would happen if you did this in the main method?



Will this work ?why will or wont it work?whats the output if any?

look in to this concept it will benefit you !!
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
from what i now understand static variables/methods are used for non object specific things?

say i was writing a program to manage a school

pupil name would be an instance variable
pupil grades would be an instance variable
pupil class number would also be instance variable

pupil count would be static right?
and uniform count could also be static

yes,dont know what i was thinking they dont have to return something

anything else you could add?
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey Ray Mann,

ok...looked at it,yes i dont see anything wrong with it,it will run and output the line "This is a non-static method saying hello world!!"

public class StaticFun{                - declares a class StaticFun

public static void main (String [] args){     // Is this Static or not? its the main method,it has the static keyword...so it must be...i dont know?  i heard on a tutorial that static means that you dont have to create an instance of it,is this correct?

sf.speak();       //Am i calling a Non Static method from the (static) main method? yes appears that way,no problem

public void speak(){    // Is this Static or not? no its not static,no static key word

System.out.println("This is a non-static method saying hello world!! "); prints the line from within the method

have i missed something?




 
Ray mann
Ranch Hand
Posts: 48
Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes well done you are correct! you should look up objects and instance. Then look up static variables vs instance variables. This will help you to understand the differences and when, how and why we use them.
 
Ray mann
Ranch Hand
Posts: 48
Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ahh sorry I just saw the previous post yes that is correct,  you could do the school like that. Although they can be specific for example the school name is specific but is shared among all pupils attending that school make sense ?

Please dont forget to use the code tags when posting questions an replies that contain code.
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey ray mann are there any tutorial projects on this site? would be great if there were...
 
Ray mann
Ranch Hand
Posts: 48
Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey jon this is what i found im sure others can point you to others is there are !
http://www.javaranch.com/java-college.jsp
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thats fantastic!!!...im off to enroll in Coderanch College!!!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!