Learn Java in One Video - 15-minute Crash Course

Coding with John
11 Aug 202114:53

Summary

TLDRIn this informative video, John, a seasoned Java programmer, empathizes with beginners' struggles and shares his journey from confusion to expertise. He guides viewers through setting up an IDE, writing basic Java programs, understanding variables, and utilizing methods. John introduces object-oriented programming concepts, explaining classes, objects, and static vs. non-static methods. His aim is to simplify Java, offering a comprehensive course for deeper insights and encouraging questions for further assistance.

Takeaways

  • 😀 The speaker empathizes with those struggling to learn Java, emphasizing that it's a common experience and not to feel alone.
  • 📚 The speaker introduces himself as John, a seasoned Java programmer who aims to share knowledge in an understandable way.
  • 💻 It's necessary to install an IDE (Integrated Development Environment) like Eclipse to write Java programs more efficiently.
  • 🛠️ Eclipse is used to write Java programs and automatically translate them into bytecode, a format the computer can understand.
  • 📝 The process of creating a Java program in Eclipse involves creating a new project and class, with the main method being the starting point for code execution.
  • 🔢 Variables in Java are used to store data and consist of a name, type, and value, with 'int' being an example of a primitive type.
  • 📑 The 'main' method is where most of the coding happens, and the code within its curly brackets defines what the program does when run.
  • 🔠 Java has eight primitive types, including 'int' for integers, 'double' for decimal numbers, and 'char' for characters.
  • 🔑 Non-primitive types like 'String' are used to hold text and offer various methods for manipulation, such as 'length()' and 'toUpperCase()'.
  • 🔄 Methods in Java can be created by the programmer to bundle functionality and avoid code repetition, and can take parameters and return values.
  • 🔒 Conditional statements like 'if-else' allow for decision-making in programs based on conditions, using '==' for equality checks.
  • 🔁 Loops, such as 'for' loops, enable repeated execution of code segments, with the loop's condition controlling the number of iterations.
  • 🐱 Java is object-oriented, allowing for the creation of classes as blueprints for objects, which can have attributes and behaviors defined by the class.
  • 📌 The 'static' keyword in Java methods means they can be called without creating an object instance, while non-static methods require an object.

Q & A

  • What does the speaker struggle with in the beginning of the script?

    -The speaker struggled with understanding basic concepts of Java and felt overwhelmed in their first Java class in college.

  • What is the purpose of an Integrated Development Environment (IDE)?

    -An IDE is a program that makes it easier to write code, similar to how Microsoft Word makes it easier to write school papers.

  • What is the name of the IDE used by the speaker in the video?

    -The speaker is using an IDE called Eclipse.

  • What does the speaker suggest to install before starting to code in Java?

    -The speaker suggests installing an IDE and possibly other tools, mentioning that the process is super easy and only takes a few minutes.

  • How does the speaker describe the process of creating a new Java project in Eclipse?

    -The speaker describes creating a new Java project by right-clicking in the Package Explorer, selecting 'New Java Project', giving it a name, and clicking 'Finish'.

  • What is a 'class' in Java according to the script?

    -A 'class' in Java is essentially a Java file, which is a fancy name for a container of code and functionality.

  • What is the significance of the 'main' method in Java?

    -The 'main' method is significant because it is the entry point of a Java program, containing the code that runs when the program is executed.

  • What is a variable in Java and what are its components?

    -A variable in Java is used to store data and has three components: a name, a type, and a value.

  • What is the difference between primitive and non-primitive types in Java?

    -Primitive types are the most basic types in Java, indicated by a lowercase letter, such as 'int' for integers. Non-primitive types are built on top of these and include more complex structures like 'String'.

  • How can you interact with a non-primitive type like 'String' in Java?

    -You can interact with a non-primitive type like 'String' by calling its methods, such as 'length()' to find the length of the string or 'toUpperCase()' to convert the string to uppercase.

  • What is a method in Java and how is it used?

    -A method in Java is a block of code that performs a specific task. It is used by declaring the method with a name and then calling it by that name within the code, potentially with parameters and a return type if necessary.

  • What is the role of 'static' in methods and classes in Java?

    -The 'static' keyword in Java means that the method or class can be called without creating an instance of the class. It is associated with the class itself rather than instances of the class.

  • How does the speaker explain the concept of object-oriented programming in Java?

    -The speaker explains that object-oriented programming in Java involves creating classes as blueprints for objects, which can then be instantiated with specific attributes and behaviors.

  • What is the purpose of conditional statements in Java?

    -Conditional statements in Java are used to perform different actions based on whether certain conditions are true or false, using 'if', 'else if', and 'else' clauses.

  • How are loops used in Java to repeat code execution?

    -Loops in Java, such as 'for' loops, are used to repeat code execution multiple times without copying and pasting the same code. They are controlled by initializing a variable, a condition to continue the loop, and an increment or decrement operation.

  • What is the speaker's final recommendation for those interested in learning more about Java?

    -The speaker recommends a full Java course available in the description of the video for those who want to go into more depth on various topics of Java.

Outlines

00:00

📚 Java Learning and Setup Essentials

The speaker empathizes with the struggles of learning Java, sharing personal experiences and offering help. The paragraph introduces the speaker, John, who has been programming in Java for 10 years and plans to share knowledge in an understandable way. It covers the basics of setting up Java development, including installing an IDE like Eclipse, which simplifies the coding process. The video also guides viewers on creating a Java project and class in Eclipse, explaining the significance of the 'main method' and how to write a simple program that prints variable values. The importance of variables, their types, and values are discussed, along with how to perform operations like multiplication and store results in new variables.

05:02

🔍 Exploring Variables, Methods, and Conditional Logic in Java

This section delves deeper into Java variables, explaining primitive types like 'int', 'double', and 'char', and their usage. It demonstrates variable interaction through example calculations and the assignment of results to new variables. The paragraph introduces non-primitive types, particularly 'String', and discusses methods associated with them, such as 'length' and 'toUpperCase'. The concept of creating custom methods in Java is introduced, explaining how methods can be used to avoid code repetition and can take parameters and return values. Conditional statements and loops, such as 'if', 'else', and 'for', are also explained, showing how they can be used to control program flow based on conditions and repeat code execution.

10:03

🐱 Object-Oriented Programming with Java Classes and Objects

The final paragraph introduces the core concept of object-oriented programming in Java, focusing on classes and objects. It explains how to create a separate class, such as 'Cat', and use it as a blueprint to create objects with unique attributes and behaviors. The paragraph demonstrates creating class methods, both static and non-static, and the difference between them in terms of accessibility and usage. It also illustrates how to set and use properties of objects, like name and age, and calls methods on them. The video aims to clarify the use of 'static' in methods and the overall object-oriented approach to structuring Java programs, offering a foundation for more complex Java applications.

Mindmap

Keywords

💡Java

Java is a high-level, class-based, object-oriented programming language designed for portability and versatility. In the video, Java is the central theme, as the speaker discusses his experience learning Java and offers guidance to beginners. The script mentions Java in the context of programming concepts and the tools needed to write Java programs, such as an IDE (Integrated Development Environment).

💡IDE (Integrated Development Environment)

An IDE is a software application that provides comprehensive facilities to computer programmers for software development. In the script, Eclipse is introduced as the IDE being used to write Java programs, highlighting its role in simplifying the coding process and converting code into bytecode that computers can execute.

💡Variables

Variables are used in programming to store data. The script explains that variables have a name, a type, and a value. For example, 'int myInt = 7;' creates a variable named 'myInt' of type 'int' (integer) with a value of 7. Variables are fundamental to Java programming, allowing data manipulation and storage throughout the script.

💡Main Method

The main method is the entry point of a Java program. It is defined with 'public static void main(String[] args)' and contains the code that runs when the program is executed. The script describes the main method as the place where most of the coding happens, illustrating its importance in Java programming.

💡Primitive Types

Primitive types in Java are the most basic data types and include 'int', 'double', 'char', etc. The script mentions 'int' for integers, 'double' for decimal numbers, and 'char' for characters. These types are essential for understanding data representation in Java and are used to define variables with specific data storage capabilities.

💡String

A String in Java is a sequence of characters used to store text. The script demonstrates creating a String variable with 'String myName = "John";'. Strings are non-primitive types and are used extensively in Java for handling text data, as shown in the video where methods like '.length()' and '.toUpperCase()' are applied to String objects.

💡Methods

Methods in Java are blocks of code designed to perform a specific task. The script introduces methods with 'private static void burp()' as an example. Methods can be called to execute their code, and they can also take parameters and return values, which is crucial for reusing code and modular programming.

💡Conditional Statements

Conditional statements are used to perform different actions based on different conditions. The script uses 'if', 'else if', and 'else' to demonstrate conditional logic, such as 'if (name.equals("John"))'. These statements are vital for decision-making in Java programs.

💡Loops

Loops are used to execute a block of code repeatedly. The script explains 'for' loops with an example that prints a phrase multiple times. Loops are essential for iterating over data and performing repetitive tasks without duplicating code.

💡Object-Oriented Programming (OOP)

OOP is a programming paradigm based on the concept of 'objects', which can contain data in the form of fields and code in the form of methods. The script touches on OOP by discussing creating a 'Cat' class and objects from that class, emphasizing the use of classes as blueprints for creating objects with specific attributes and behaviors.

💡Static

The 'static' keyword in Java is used to create class-level methods or variables that are not tied to any specific instance of the class. The script mentions 'static' in the context of the 'dingDong()' method, which can be called on the 'Cat' class itself without needing a 'Cat' object. This is contrasted with non-static methods like 'meow()' that require an instance of the class to be called.

Highlights

The speaker empathizes with those struggling to learn Java, emphasizing that they are not alone.

John, the speaker, shares his personal experience of struggling with Java in college but now has 10 years of experience.

The importance of installing an IDE like Eclipse for writing Java programs is discussed.

Eclipse is introduced as the IDE used for writing and translating Java programs into bytecode.

A step-by-step guide on creating a Java project and class in Eclipse is provided.

Explanation of the 'main method' in Java and its role in running a program.

Introduction to variables, their types, and values in Java with examples.

The concept of primitive types in Java and their characteristics is explained.

Demonstration of how to perform operations like multiplication on variables and store results.

Introduction to non-primitive types like 'String' and their usage in Java.

Explanation of methods, how to call them, and their functionality in Java.

The process of creating custom methods in Java and their purpose is outlined.

Parameters and return values in methods are introduced with examples.

Conditional statements and their syntax in Java are explained with examples.

The concept of loops, specifically 'for loops', and their use in Java is discussed.

Java's object-oriented programming nature and the use of classes as blueprints for objects.

Creating objects from classes and setting their attributes like name and age.

The difference between static and non-static methods in Java and their usage.

John offers further help for any Java questions in the comments and provides a link to a full Java course.

Transcripts

play00:00

if you're struggling learning java i

play00:01

understand how you're feeling it can

play00:02

feel like everyone around you is some

play00:04

kind of programming genius who just gets

play00:06

everything with no problem at all and

play00:07

you feel like you must be the only one

play00:09

having trouble with it you're absolutely

play00:10

not alone and you're in the right place

play00:12

i remember in my first java class in

play00:14

college i really struggled to understand

play00:16

some of just the basic concepts and

play00:18

really just felt overwhelmed but now

play00:20

i've been programming in java for a

play00:21

living for the past 10 years and i want

play00:23

to make sure you don't have to struggle

play00:24

the same way i did my name's john and i

play00:26

love sharing what i've learned about

play00:27

java in a clear and understandable way

play00:29

so please consider subscribing if you'd

play00:30

like to see more helpful java videos in

play00:32

this video we'll go over everything you

play00:34

need to know to get started writing

play00:35

great java programs before you can code

play00:37

anything in java there's a couple of

play00:38

things you have to have installed if

play00:39

you've already done that awesome and if

play00:41

not it's super easy it just takes a few

play00:42

minutes i have a video here going

play00:44

through the whole process one of the

play00:45

things you'll want to install is called

play00:46

an ide and integrated development

play00:48

environment and that's what i have open

play00:49

here the name sounds really complicated

play00:51

but it's basically just a program that

play00:52

makes it easier to write code kind of

play00:54

like microsoft word makes it easier to

play00:56

write school papers here the ide that

play00:57

i'm using is called eclipse basically

play00:59

we're going to write our java programs

play01:01

here in eclipse and then it is going to

play01:02

take our programs and automatically

play01:04

translate them into a format the

play01:05

computer can understand something called

play01:07

byte code you know essentially the ones

play01:08

and zeros that make the computer happy

play01:10

and it's going to run it when you open

play01:12

eclipse to get started creating a java

play01:13

program just right click here in this

play01:15

area called uh the package explorer and

play01:17

go to new java project you can give it

play01:19

whatever name you want we'll just call

play01:20

it a project pineapple you can uncheck

play01:23

this module thing down here we don't

play01:24

need that and click finish you can see

play01:26

it created our project for us here

play01:27

project pineapple then you can expand

play01:29

this drop down and right click this src

play01:31

folder here which stands for source and

play01:33

go to new class a class is basically

play01:36

just a fancy name for a java file give

play01:37

that name to whatever you want like

play01:39

awesome java program and you want to

play01:41

check this box down here that says

play01:42

public static void main string args

play01:45

we'll talk a little more about what that

play01:46

means later then click finish and then

play01:47

eclipse will automatically generate this

play01:49

kind of framework for a working java

play01:51

program for you now all of these

play01:52

programming looking terms and keywords

play01:54

might feel really intimidating but

play01:55

really all this is saying is hey java

play01:57

i've got this new program called awesome

play01:59

java program and when i tell you to run

play02:01

here's all the stuff that i want you to

play02:02

do this whole section here is what we

play02:03

call the main method and all the

play02:05

commands we put in between these two

play02:07

curly brackets in the main method are

play02:08

what it's going to do when we tell it to

play02:10

run so that's where we're going to be

play02:11

writing most of our code this line here

play02:12

with the two slashes is just an

play02:14

automatically generated thing that we

play02:15

don't need so we can just get rid of it

play02:16

just about everything we'll do in java

play02:18

revolves around creating and using

play02:20

things called variables variables are

play02:22

just used to store data so here's an

play02:23

example and myint equals seven now

play02:27

variables have a name a type and a value

play02:30

for this variable int is the type it

play02:32

stands for integer int is basically just

play02:34

a number this my int is the name of the

play02:36

variable we can give it whatever name we

play02:37

want and this equals seven here is

play02:39

giving this my into the value of seven

play02:41

so this just says hey java i have a

play02:43

number here i want to call it myant and

play02:45

please give it the value of seven to

play02:47

start out with and then we have this

play02:48

semicolon in java most lines will end

play02:50

with a semicolon it's just telling java

play02:52

that's the end of the statement now

play02:53

anywhere after that line in our program

play02:55

we can get the value stored in that

play02:57

variable just by using its name for

play02:59

example if we want to just print it out

play03:00

the command to print something out in

play03:02

java is system.out.println

play03:06

and in parentheses we specify the name

play03:08

of that variable myant and we end with a

play03:10

semicolon and we can click save and then

play03:12

click run to run our program and we see

play03:14

our output of the program here in our

play03:16

console so it just prints out the value

play03:18

of this my int variable which is seven

play03:20

int isn't the only type of variable we

play03:22

can create for example if you want to

play03:23

create a decimal number you can use

play03:25

double let's say we want to make a shoe

play03:26

size double shoe size equals 9.5 if you

play03:29

want to store a character you can use

play03:31

the char type char my initial equals j

play03:35

that'll be in single quotes a lot of the

play03:37

magic encoding happens when we take

play03:39

these variables and have them interact

play03:40

with each other so we can take this

play03:42

mayan variable and multiply it by the

play03:44

shoe size if we want in java to multiply

play03:46

you just use star so my int times shoe

play03:49

size save and run it we see that we get

play03:51

the result 66.5 which is 7 times 9.5 and

play03:54

also instead of just printing out that

play03:56

result you can actually store the result

play03:59

of that calculation in another variable

play04:01

so we can say double result equals my

play04:03

int times shoe size all of these types

play04:06

so far and double and char those are

play04:08

called primitive types in java there are

play04:10

eight primitive types total in java and

play04:12

i have a video going over all of them

play04:13

and how to use them in detail here

play04:15

primitives are like the absolute basic

play04:17

types in java like the sticks and stones

play04:19

you'll know it's a primitive type in

play04:20

java if it starts with a lowercase

play04:22

letter every other type you use in java

play04:24

is built on top of these primitive types

play04:26

but there are tons of other types you

play04:28

can use and you can even create your own

play04:29

like we'll see later probably the

play04:31

non-primitive type that you're going to

play04:32

use most is called string string

play04:35

variables are used to hold text string

play04:37

my name equals john for strings you put

play04:40

them in double quotes instead of single

play04:42

quotes you can tell that string is not a

play04:43

primitive type because it starts with a

play04:44

capital s and also the primitive types

play04:46

have a different coloring in eclipse

play04:48

than other types do when you create a

play04:49

non-primitive variable like this there's

play04:51

also even more stuff you can do with it

play04:53

that can be different for each type to

play04:54

see what you can do with the variable if

play04:56

you're using a fancy ide like eclipse

play04:58

you can start out by just typing the

play04:59

variable name then type a period and

play05:01

you'll immediately see all the cool

play05:03

things you can do with this variable

play05:04

these are all called methods and when we

play05:06

use any of them it's called calling a

play05:08

method as you're scrolling through these

play05:10

you can click one and you'll get a

play05:11

description of what that method does for

play05:13

example this length method says it

play05:15

returns the length of this string and if

play05:16

we want to use it we just double click

play05:18

it or you can also just type it out if

play05:19

you want to so if we move this down here

play05:21

so we can print out the result of this

play05:24

method call it prints out four because

play05:26

john the value of the my name variable

play05:28

is four characters long but there are a

play05:30

ton of other methods available like for

play05:31

example there's one called a two

play05:33

uppercase and if we use that it gives

play05:35

you an all uppercase version of that

play05:37

string and if you already have the

play05:38

method out here in your code and you

play05:40

want to remind yourself what it does you

play05:41

can just hover over the method name and

play05:43

a little pop-up will appear with the

play05:44

description one thing you can't do is

play05:46

you can't call a method on a primitive

play05:48

variable like my interior if i try to do

play05:50

my int dot nothing pops up it just

play05:52

doesn't work speaking of methods you can

play05:54

also create your own to create a new

play05:56

method here in the same java file i'll

play05:57

type it out first and then we'll talk a

play05:58

bit about what it means private static

play06:01

void burp this whole line here is called

play06:03

the method declaration and this part

play06:05

right here before the parentheses is the

play06:07

name of the method here we called it

play06:08

burp and any code that we put between

play06:10

these two curly brackets is what it's

play06:12

going to do when this method is called

play06:13

let's just have it print out

play06:15

burp but if we go ahead and save and run

play06:18

our code now nothing happens nothing's

play06:20

printed out why is that that's because

play06:22

if you remember when we run our java

play06:24

program java just runs everything that

play06:26

we put inside our main method and right

play06:28

now there's nothing in here that calls

play06:30

this other method that we created so

play06:31

let's change that now if we want to call

play06:33

this method all we have to do is use its

play06:35

name burp and we also need an open and

play06:38

close parentheses whenever you see this

play06:39

open and close parentheses you'll know

play06:41

that it's a method being called now if

play06:43

we save and run our program successfully

play06:45

burps so now we can have our program

play06:47

burp whenever we want just by calling

play06:49

burp of course a method can be a whole

play06:50

lot more complicated than this and a big

play06:52

reason to make a method is because you

play06:54

might have a whole big complicated chunk

play06:56

of code that you don't want to repeat

play06:58

package it up in a method and you can

play06:59

just call it as many times you want from

play07:01

anywhere in your code just by using its

play07:02

name now a method can also take what is

play07:05

called parameters so for example if this

play07:07

method was called print name between

play07:09

these two parentheses is where you would

play07:11

put the parameters let's say it had a

play07:13

string parameter that was called name

play07:15

and then just printed out my name is

play07:18

plus name this plus is how you link two

play07:21

strings together in java that's called

play07:23

concatenating strings but now when you

play07:25

call this print name method you have to

play07:27

pass in a value for that name parameter

play07:29

and you pass in that value by putting it

play07:31

between these two parentheses uh john

play07:33

and we can run it and see that it prints

play07:35

out my name is john you can even take

play07:37

multiple parameters if you want just

play07:38

separate them by commas so i can also

play07:40

take in like int number and when you're

play07:42

calling it you just do the same thing

play07:44

you just separate the values that you're

play07:45

passing in with a comma if you want you

play07:47

can also have your method return a value

play07:49

right here in the method declaration

play07:51

where we have this void void means it's

play07:53

not returning any values but if you want

play07:55

it to return a value this is where we

play07:56

specify the type of the thing that it's

play07:58

going to return so if we want it to

play08:00

return a string we can just change this

play08:01

to a string but now we have to actually

play08:03

return a string somewhere in our method

play08:04

so what we can do instead of just

play08:06

printing out my name is blah we can just

play08:08

change this to return

play08:10

that value my name is blah the code that

play08:13

calls this can do whatever it wants with

play08:15

the string that's returned for example

play08:16

we could just print it out ourselves

play08:17

here or if we want since this returns a

play08:20

string we can actually save that

play08:21

resulting value in our own string

play08:23

variable so string name equals the

play08:25

result of this method call one of the

play08:27

most useful things you'll ever use in

play08:28

java is called conditional statements

play08:30

that's your basic if this then that

play08:32

conditional statements in java will look

play08:34

like this if whatever condition we put

play08:36

in here is true run this stuff otherwise

play08:39

if whatever condition we put in here is

play08:41

true run this stuff and if neither one

play08:43

of those are the case run this and you

play08:45

don't necessarily need all three pieces

play08:47

for this you might just have an if you

play08:49

might just have an if else you don't

play08:50

need all three so what kind of

play08:51

conditions can we have in here so we

play08:53

could say like if name equals john then

play08:57

print this guy is awesome else if the

play09:00

name equals larry this guy is okay i

play09:03

guess and if it's not john and not larry

play09:06

prince i don't know this guy at all this

play09:08

example uses strings as its conditions

play09:10

but if you want to use the like a number

play09:12

as the condition you can do things like

play09:14

if number

play09:15

double equals five in java you use

play09:17

double equal to check equality like this

play09:19

that's because the single equals is

play09:20

already being used for assigning values

play09:22

to variables so just remember to use

play09:24

double equals but with numbers you don't

play09:25

always just have to check for equals you

play09:27

can do other things like if the number

play09:28

is greater than 5 or less than 5 or if

play09:31

you want to say if the number is not

play09:32

equal to 5 you use exclamation point

play09:35

equals next if you want to be able to

play09:37

repeat a piece of code a whole bunch of

play09:38

times without actually copying and

play09:40

pasting the same code over and over and

play09:42

over again you can use what's called a

play09:44

loop one type of loop in java is a for

play09:46

loop and it looks like this and i equals

play09:48

0 i less than 10 i plus plus what this

play09:51

will do is run everything inside these

play09:53

curly brackets 10 times how it does that

play09:55

is it declares this variable i and says

play09:58

hey keep looping while i is less than

play10:01

10. and every time it goes through the

play10:02

loop increment i by one that's what this

play10:05

i plus plus does so we can have it print

play10:07

out these pretzels are making me thirsty

play10:11

if we run it it'll print out these

play10:13

pretzels are making me thirsty 10 times

play10:15

and we can have this for loop do this

play10:17

however many times we want so if we

play10:18

change this 10 to a thousand and run our

play10:21

program it'll print it out a thousand

play10:23

times one huge thing about java is that

play10:25

it's an object oriented programming

play10:27

language a big part of being object

play10:28

oriented is that you can take a whole

play10:30

bunch of code and put it in another java

play10:32

file called a class and you can use that

play10:34

code in some other java file like your

play10:36

main file so let's do that now we can go

play10:38

over and right click on source again and

play10:40

go to new class say we want to create a

play10:43

cat class so we'll call it cat and you

play10:45

can create a method in your class like

play10:46

this public static void ding dong which

play10:50

prints out ding dong this public keyword

play10:52

here makes it so this method can be

play10:54

called from any other java file if you

play10:56

remember in our other method we used

play10:58

private private makes it so this method

play11:00

can only be called from within this file

play11:02

that was fine because that's all we

play11:03

needed it for but if you make it public

play11:05

it can be used anywhere so now we can

play11:07

call this cat ding dong method from our

play11:09

main program like this we just say cat

play11:11

dot ding dong so you can absolutely use

play11:14

a separate class just like this a place

play11:16

to put other methods so they don't

play11:18

clutter up your main class but the main

play11:20

use of a separate class like this is

play11:22

actually as a blueprint to create what

play11:24

are called objects so here we have a cat

play11:27

class now this class itself isn't a cat

play11:30

object but we can use this class to

play11:32

create cat objects as many as we want

play11:34

and here's how so back in our main

play11:35

program we can just say cat i cat equals

play11:39

new cat this creates a brand new cat

play11:42

object and stores it in this variable my

play11:44

cat and we can create another one if we

play11:45

want so cat

play11:46

another cat equals new cat right now we

play11:49

haven't really put much of anything in

play11:51

this cat class so our individual cats

play11:53

that we've created from it can't really

play11:54

do a whole lot but we can change that so

play11:56

we know a cat in real life probably has

play11:58

a name and an age and it can meow right

play12:00

we can make our cat class like that too

play12:02

so that the cats we create can do those

play12:04

things so here's how we do that we just

play12:06

add string name int age and we'll create

play12:10

a new method public void meow that

play12:13

prints meow now note that we didn't

play12:16

actually set a value for the name or the

play12:19

age here remember the class isn't a cat

play12:21

in itself it's a blueprint for creating

play12:23

cats and it tells you what kind of

play12:24

attributes it has and what it can do so

play12:26

what this is saying is that each

play12:27

individual cat made from this class can

play12:29

have a name it can have an age and it

play12:32

can do this thing it can meow and back

play12:34

here where we're actually creating our

play12:36

individual cats that's when we can

play12:38

actually set the name and age of these

play12:41

individual cats my cat dot name equals

play12:44

fred my cat.age equals six we use this

play12:48

dot to access the fields on these

play12:51

objects very similarly to how we call

play12:53

methods but you'll know the difference

play12:54

because when we call methods we always

play12:56

have an open and close parentheses and

play12:58

when we're accessing a field on that

play13:00

class like name or age we don't have any

play13:02

parentheses and on another cat a whole

play13:04

separate cat object we can set its own

play13:06

values so another cat.name equals stella

play13:09

stella's age is five later on in our

play13:12

program we can get those values and do

play13:13

whatever we want with them so if we want

play13:15

to print out fred's age later we can do

play13:16

that icat dot age and it prints out six

play13:19

one huge point of confusion for me

play13:21

personally when i was learning java is

play13:23

this whole static thing you might have

play13:26

noticed in our methods sometimes we use

play13:28

this static keyword and sometimes we

play13:30

didn't all this static keyword means is

play13:33

that this method can be called without

play13:36

using an individual cat object for

play13:39

example we made this ding dong method

play13:41

static so back here in our main method

play13:43

we could just call cat dot ding dong

play13:46

notice we're just calling this method on

play13:48

the clasp itself not on any specific cat

play13:50

object that we created we don't need to

play13:52

create a cat object first if we just

play13:54

want to use this method but if it's not

play13:56

static like this meow method you can

play13:58

only call that method using an

play13:59

individual cat object so back here

play14:02

notice that i can't call

play14:04

hat.meow just using the class name it

play14:06

gives me an error that says i can't make

play14:08

a static reference to this non-static

play14:10

method but down here where i've actually

play14:12

created a cat object like my cat i can

play14:15

call meow on that just fine whether a

play14:17

certain method that you make is static

play14:19

or non-static just depends on what makes

play14:21

sense for what you're trying to do of

play14:22

course java is much deeper than what can

play14:24

be covered in one video like this but

play14:26

this should give you a ton of tools that

play14:27

you can use and build off of to start

play14:29

making some awesome programs i do have a

play14:31

full java course available in a link

play14:33

down in the description if you're

play14:34

interested that goes way more into depth

play14:36

on a ton of topics but if not that's

play14:37

awesome too i'm just thrilled to have

play14:39

you here with me my goal is really just

play14:41

to help as many people as i possibly can

play14:43

for real if you ever have any sort of

play14:44

java questions that i might be able to

play14:45

help with just put them in the comments

play14:47

and i'll do my best to respond thanks so

play14:48

much for watching i'm so glad to have

play14:50

you here with me and i'll see you in the

play14:52

next video

Rate This

5.0 / 5 (0 votes)

Связанные теги
Java LearningIDE SetupProgramming BasicsObject-OrientedCode TutorialEclipse IDEVariablesMethodsConditionalsLoopsClassesJava Syntax
Вам нужно краткое изложение на английском?