Methods Introduction

Complete Step By Step Java for Testers Classes And Methods -> Object Oriented Concepts
9 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€93.77
List Price:  €131.29
You save:  €37.51
£80.84
List Price:  £113.18
You save:  £32.34
CA$137.53
List Price:  CA$192.54
You save:  CA$55.01
A$155.84
List Price:  A$218.18
You save:  A$62.34
S$136.11
List Price:  S$190.56
You save:  S$54.45
HK$783.07
List Price:  HK$1,096.33
You save:  HK$313.26
CHF 91.04
List Price:  CHF 127.46
You save:  CHF 36.42
NOK kr1,101.67
List Price:  NOK kr1,542.39
You save:  NOK kr440.71
DKK kr700.10
List Price:  DKK kr980.18
You save:  DKK kr280.07
NZ$169.82
List Price:  NZ$237.75
You save:  NZ$67.93
د.إ367.23
List Price:  د.إ514.14
You save:  د.إ146.90
৳10,949.65
List Price:  ৳15,329.95
You save:  ৳4,380.30
₹8,336.20
List Price:  ₹11,671.02
You save:  ₹3,334.81
RM478.35
List Price:  RM669.71
You save:  RM191.36
₦129,687.03
List Price:  ₦181,567.03
You save:  ₦51,880
₨27,766.58
List Price:  ₨38,874.33
You save:  ₨11,107.74
฿3,679.68
List Price:  ฿5,151.71
You save:  ฿1,472.02
₺3,248.23
List Price:  ₺4,547.65
You save:  ₺1,299.42
B$520.25
List Price:  B$728.38
You save:  B$208.12
R1,929.28
List Price:  R2,701.08
You save:  R771.79
Лв183.27
List Price:  Лв256.58
You save:  Лв73.31
₩137,435.75
List Price:  ₩192,415.55
You save:  ₩54,979.80
₪376.07
List Price:  ₪526.51
You save:  ₪150.44
₱5,756.72
List Price:  ₱8,059.64
You save:  ₱2,302.91
¥15,460.95
List Price:  ¥21,645.95
You save:  ¥6,185
MX$1,709.87
List Price:  MX$2,393.89
You save:  MX$684.02
QR364.04
List Price:  QR509.67
You save:  QR145.63
P1,385.73
List Price:  P1,940.08
You save:  P554.35
KSh13,119.90
List Price:  KSh18,368.39
You save:  KSh5,248.48
E£4,832.51
List Price:  E£6,765.71
You save:  E£1,933.20
ብር5,668.96
List Price:  ብር7,936.77
You save:  ብር2,267.81
Kz83,691.63
List Price:  Kz117,171.63
You save:  Kz33,480
CLP$95,228.57
List Price:  CLP$133,323.80
You save:  CLP$38,095.23
CN¥723.85
List Price:  CN¥1,013.42
You save:  CN¥289.57
RD$5,936.90
List Price:  RD$8,311.90
You save:  RD$2,375
DA13,457.95
List Price:  DA18,841.67
You save:  DA5,383.72
FJ$227.72
List Price:  FJ$318.82
You save:  FJ$91.09
Q775.98
List Price:  Q1,086.41
You save:  Q310.42
GY$20,874.15
List Price:  GY$29,224.64
You save:  GY$8,350.49
ISK kr14,123.58
List Price:  ISK kr19,773.58
You save:  ISK kr5,650
DH1,010.74
List Price:  DH1,415.07
You save:  DH404.33
L1,790.82
List Price:  L2,507.22
You save:  L716.40
ден5,773.75
List Price:  ден8,083.48
You save:  ден2,309.73
MOP$804.95
List Price:  MOP$1,126.96
You save:  MOP$322.01
N$1,917.07
List Price:  N$2,683.98
You save:  N$766.90
C$3,695.63
List Price:  C$5,174.03
You save:  C$1,478.40
रु13,325.78
List Price:  रु18,656.63
You save:  रु5,330.84
S/372.26
List Price:  S/521.17
You save:  S/148.91
K380
List Price:  K532.02
You save:  K152.01
SAR375.10
List Price:  SAR525.15
You save:  SAR150.05
ZK2,561.66
List Price:  ZK3,586.43
You save:  ZK1,024.76
L466.89
List Price:  L653.66
You save:  L186.77
Kč2,369.71
List Price:  Kč3,317.69
You save:  Kč947.98
Ft36,989.30
List Price:  Ft51,786.50
You save:  Ft14,797.20
SEK kr1,091.25
List Price:  SEK kr1,527.80
You save:  SEK kr436.54
ARS$87,116.28
List Price:  ARS$121,966.28
You save:  ARS$34,850
Bs690.90
List Price:  Bs967.28
You save:  Bs276.38
COP$391,948.05
List Price:  COP$548,742.95
You save:  COP$156,794.90
₡50,105.09
List Price:  ₡70,149.13
You save:  ₡20,044.04
L2,463.18
List Price:  L3,448.55
You save:  L985.37
₲738,132.47
List Price:  ₲1,033,414.98
You save:  ₲295,282.51
$U3,839.04
List Price:  $U5,374.81
You save:  $U1,535.77
zł404.45
List Price:  zł566.25
You save:  zł161.79
Already have an account? Log In

Transcript

Hello all welcome back to the next one. In this one we will understand about the methods concept in Java. So let me bring up Eclipse. And we do have a source folder here. Let me first create a package up here. So let's create a new package called maybe I'll just call it like object.

Or maybe I'll just say like oops concepts. And that should be good. So old concept is object oriented programming concepts. Oops, concepts is fine, are maybe just let's get rid of. So now we'll create a class. We have been constantly creating classes and methods like by default in all the lectures that we have covered so far.

So do not worry about classes right now at this point, what this means or what are the details, we will have separate lectures which is going to cover classes in detail. So for now, let's concentrate on that. That's, so I'm gonna give it a name called maybe methods demo. And I click this guy, which is public static void Main. And click Finish. So it creates a class for me, it creates some method for me.

So this public static void Main is also a method and we'll talk about it. So what are methods like methods are really go by the name. This is a method to do something. That's how we use it in our real life methods are the way to do something, all the actions, code statements, and everything in Java will go under methods. If there are no methods, we cannot actually write the actions which we want to perform. We can define variables outside of the method, but we cannot write any actions outside of the method.

It is really important to have method nothing can go outside of the method. So let's say I can define a variable like string a equals to I can give it a value also, hey, there are something I can do that, but I cannot use this variable anywhere, I cannot even print it. So, so let's let's do that. If I'm gonna write print statement, and let's say one of printed here. And it could also now let's see what it says. So it says, because this is not static, so we will talk about static in different lectures, but don't worry about it.

So you'll see these things right public static void. Do not worry about these things. I know public is police straightforward. It's public, it can be viewed from anywhere, but we have a separate lecture covering Public Private productor static void, we will cover in future lectures in detail. So we define that and now we can run it. So let me just quickly run it and show you so run the Java application.

And it's gonna print a there. But let's try to copy this guy and put it here. does it allow me it does not, we get the error message right away. It says multiple markers at this line and syntax error tokens, misplaced constructs will give us all sorts of errors. The reason is, it does not allow us to perform any actions outside of outside of the method. The only thing we can do outside of the methods is we can define or declare variables, that's it.

Otherwise nothing can go outside of the method. So that's method like everything in Java, every statement, every logic, all the code logic, anything that we want to perform should be written inside the method. So every Java class needs a main method. This is one important requirement right now we are seeing method every Java class needs a main method. Main method is like the entity point of the class if there is no main method, the class cannot be run on its own. It can be called in other classes, but it cannot be run on its own.

What I mean by that is, let's call it as a, you know what, let's copy it out and paste in and change the name. So maybe method one. And let's comment this code block, which is my main method, right? And I am still doing the same thing. If you notice that I am printing out the variable a. And you know what, also let's define it inside the class.

Right now, let's not put these things let's not mix up the concepts. And let's remove this. So right now we are defining it inside the method. And I'm just going to try and run this. So then as I don't even get it Anything to run? It doesn't even give me an option to run the Java application.

Why is it happening? Because Eclipse itself identifies that there's no main method and you cannot run this class alone. You can call these methods this class into another class. But you cannot run this class and wherever you call this class, that can also only be run if it has been method. So let's see the magic. If I uncomment this out, I'll be able to see everything.

So let me just define one more thing here. So then we get rid of the error. Now if I right click, I'll see run as Java application. So now it knows that it's a Java application. It has a main method and we can run it. So basically, we can have any number of methods inside a class, but the class really need a main method to run otherwise we cannot run a Java class individually.

So, like this main method, we can create any number of methods like we created this one, we can create method to method three or we can give any name to the method, the preferences always give method num one like always give the naming convention as camelcase start from the small errors and the second word should have capital letter. So this is the, this is the usual coding convention, Java always start with camelcase. And this is not needed here, actually. So let me put this away. And the convention is for the main method, in Java, we have to provide string, this is a array arcs. So we have to provide string arcs array in the main method.

This is actually a legacy code. We don't use it right now. Like it's not even used when we run this Java code. But previously Java wanted this and this is a Because it is usually used if we want to run this same method outside of the eclipse, maybe from a command line, we can actually kind of provide arguments to this main method, which is not used heavily right now, because usually, we don't depend on the arguments getting from the command line. So just understand that this is a legacy code, we don't, it really isn't used anywhere in this once we haven't used it. So that's about it.

And we should not write all the code in main method, that's one very important thing. Our code should always be modular, and we should have methods for every action or workflow that we want to perform. That is really important. So, always keep that in mind and we will do that in future lectures. So we will write separate code and we will work around the core and do all those kind of things. Other thing is we cannot put methods inside a method like we cannot do this.

I cannot just put them in In signup method, it will start giving me errors, and just say so many things, right? So don't do that method. Every method should be in a class but not inside each other. That's very important. And these are the key words that we already talked about, we will have the future discussion on these, and in other lectures, so do not worry about it. So that's about it right?

Now, you know what, let me just kind of comment this out. Or maybe I can leave it as well. So and in the next lecture, we're going to talk a lot more about the methods. I'm going to write a simple code inside the method then I'm going to show you what are the different ways Why do we create different methods and how do we call those methods inside the main method so that we can execute them? Because right now when we run it, you will notice that it's only printing hey there once it's not printing from the method num right? And We will get rid of this method in the next lecture.

And we will create separate methods and we'll see how we call methods into another methods and how we basically go about with the classes and methods, how it's useful. So Alright, that's kind of a good introduction. Please kind of practice this small thing. Just download the code. And let me know if you have any questions I'll see in the next lectures. Thanks for attending this one.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.