---
title: "Java: Classes"
date: 2015-01-01
description: "## Quick summary * a class is a template used for creating objects * must be initialized with an object to use * can be initialized with arguments * uses..."
canonical_url: https://idratherbewriting.com/java-classes/
---
# Java: Classes
## Quick summary

 - a class is a template used for creating objects

 - must be initialized with an object to use

 - can be initialized with arguments

 - uses Upperclass camel case

 - class name must match file name

 - you can only have 1 *public* class per Java file, but you can have multiple non-public classes in the same file

 - contains fields (primitives or object references) and methods

## Eclipse example

 - object_example

## Example

`Dog.java`:

```java
package dog;

public class Dog {

 String breed;

 int age;
 int size;

 String color;

 void bark(){ 
 System.out.println("bark bark bark");
 }

 void hungry(){ }

 void sleeping(){ }

}

`App.java`:

```java
package dog;

public class App {

 public static void main (String[] args) {

 Dog d = new Dog(); // make a new object
 d.bark(); // call the bark method on my object
 d.size = 40; // set the size for the object
 }

}
```